Sanjay Jain

Hi All,

Could anyone please suggest how I can call the c# dll methods in vc++ dll

Thanks,



Re: Smart Devices Native C++ Development Using C# Dll in VC++ Dll

Sanjay Jain

Hi All,

Could anyone please suggest how can I call the c# dll methods in vc++ dll and test it on Windows Mobile 5.0

Thanks,





Re: Smart Devices Native C++ Development Using C# Dll in VC++ Dll

Saravanan V V

Hi
There is nothing like C# Dll and VC++ Dll, all Dll are same. You can call the exported functions by

1.Create function pointer of the method you are going to invoke from the dll
2.Create a varibale for the function pointer.
3.Load the library - LoadLibray() API
4.Get the method address - GetProcAddress() API and assign to function pointer variable
5.Use the varible and call it wiht the parametrs of the function.
6.Unload the library - FreeLibrary() API




Re: Smart Devices Native C++ Development Using C# Dll in VC++ Dll

Sanjay Jain

Hi Saravanan,

Thanks for your prompt response. But I created the C# Dll from VS2005 and as you know there is no way to specify any method with Export keyword same like we do in vc++ .def.

So how unmanaged dll can use the methods of managed dll.

If I am wrong then correct me.

Thanks,





Re: Smart Devices Native C++ Development Using C# Dll in VC++ Dll

Saravanan V V

Hi

Sorry for the above information. You can call a C# dll in a c++ using the COM interop/COM callable wrapper. So you are correct. Pls check the following link for your guidence.

Exposing .NET Framework Components to COM
http://msdn2.microsoft.com/en-us/library/zsfww439.aspx

COM Interop Sample: COM Client and Windows Server 2003
http://msdn2.microsoft.com/en-us/library/2w30w8zx.aspx





Re: Smart Devices Native C++ Development Using C# Dll in VC++ Dll

Sanjay Jain

Hi

I gone through the above links you sent. But how can I create the .tlb file as I don't see any regasm.exe in Windows Mobile 5.0 SDK or even VS 2005 directory. As the doc says I have to create the tlb file with regasm.exe, so could you plz clarify here or am I missing something.

Thanks again,





Re: Smart Devices Native C++ Development Using C# Dll in VC++ Dll

Ilya Tumanov

There¡¯s no way to call managed code from native on NETCF since NETCF does not support hosting.






Re: Smart Devices Native C++ Development Using C# Dll in VC++ Dll

Michael Koster

There is an 'indirect' way of calling C# methods from native code.

From your C# code register delegates as callback functions with your C++ code. Implement these callbacks as function pointers in the C++ code.

Details on how to do this is documented in the MSDN docs.

Hope this is an option for you

Michael






Re: Smart Devices Native C++ Development Using C# Dll in VC++ Dll

Sanjay Jain

Hi Michael,

Thanks for the reply. As you said I searched through MSDN but can't really find anything helpful. As you said I registered the delegates in c# as callback functions with C++ code. Which looks like below:

vc++

=========================================================================

typedef void (__stdcall *PFN_MYCALLBACK)();

int __stdcall MyFunction(PFN_MYCALLBACK callback)

{

MessageBox(NULL,L"MyFunction is called",L"Result From FusionCore",MB_OK);

return 0;

}

=========================================================================

C#

=========================================================================

public delegate void MyCallBack();

public class Core

{

[DllImport("HSDataManager.dll")]

public static extern void MyFunction(MyCallBack callback);

}

=====================================================================================

Now the question is how to call c# dll' code in to vc++ dll. I am confused here and got no idea about this.

Thanks,

sanjay





Re: Smart Devices Native C++ Development Using C# Dll in VC++ Dll

Michael Koster

Hi Sanjay

Here is a small sample that shows how to register a managed callback (delegate) with native code.

Here's the managed code side:

Code Snippet

public delegate void ManagedCallbackDelegate (int aParam);

public partial class Form1 : Form

{

[DllImport ("InteropLib.dll")]

private static extern void NativeRegister (ManagedCallbackDelegate callback);

[DllImport ("InteropLib.dll")]

private static extern void NativeCall ();

//define a variable holding a reference to the delegate passed into the native code

// to make sure the callback is allways reachable

private ManagedCallbackDelegate myCallbackDelegate;

public Form1 ()

{

InitializeComponent ();

myCallbackDelegate = new ManagedCallbackDelegate (MyCallback);

//Register callback with native code

NativeRegister (myCallbackDelegate);

//have the native code call our managed callback

NativeCall ();

}

// the actual managed callback called by native code

private void MyCallback (int a)

{

MessageBox.Show (a.ToString ());

}

}

Here's the native code side:

Code Snippet

//function pointer definition matching the managed delegate

typedef void (*fpCALLBACK) (int aParam);

//static variable holdung 'managed' callback

static fpCALLBACK s_pCallback = NULL;

//register API

__declspec (dllexport) void NativeRegister (fpCALLBACK p_pCallback)

{

s_pCallback = p_pCallback;

}

//this is a dummy function simulate native code

__declspec (dllexport) void NativeCall ()

{

//call our managed callback

s_pCallback (1);

}

Hope this is clear enough

Michael






Re: Smart Devices Native C++ Development Using C# Dll in VC++ Dll

Sanjay Jain

Hi Michael,

The code got worked when I created the C# console exe and called the native methods from its Main() method, but the real question is if I make the c# dll (Class Library) then how I can call the Dll' methods in vc++ dll. Bc in this scenario, the c# is the console exe, so I can start the exe through CreateProcess() API, but if it is dll there is no such Main() method and in this case how I can access the c# dll method from vc++.

Thanks for your kind support,

sanjay





Re: Smart Devices Native C++ Development Using C# Dll in VC++ Dll

Ilya Tumanov

Again, there's no way to do that. You can only use managed code from managed process. You can even call back into managed code from native but EXE must be managed.Native EXE can't use managed DLL.






Re: Smart Devices Native C++ Development Using C# Dll in VC++ Dll

Sanjay Jain

Hi Ilya,

Thanks for the info. As you said, if I can't do that then I got only one last option. I created the C# COM dll which got *.tlb generated via VS2005. Now I want to use this C# COM dll in vc++ (native) exe. Here is my code.

=============================================================================

#import "DLL.tlb" named_guids

int _tmain(int argc, _TCHAR* argv[])
{

HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED );

if(hr == S_OK)
{
MessageBox(NULL, L"Success", L"Accessing Managed DLL", MB_OK);
}
else
{
MessageBox(NULL, L"Failure", L"Accessing Managed DLL", MB_OK);
}

DLL::ICore* pInterface = NULL;

HRESULT result = CoCreateInstance(DLL::CLSID_Core, NULL, CLSCTX_INPROC_SERVER,
DLL::IID_ICore, reinterpret_cast<void**>(&pInterface));

if(result == S_OK)
{
MessageBox(NULL, L"Success", L"Accessing Managed DLL", MB_OK);

}
else
{
MessageBox(NULL, L"Failure", L"Accessing Managed DLL", MB_OK);
}

=================================================================================

But it always fails at CoCreateInstance(). While debugging through VS I got the error saying "Class not registered".

So could you plz help me in this matter Also I got one more question, which all the files I have to package in to CAB and where they all have to be stored on targetted device

Thanks,

sanjay