Hi All,
Could anyone please suggest how I can call the c# dll methods in vc++ dll
Thanks,
Hi All,
Could anyone please suggest how I can call the c# dll methods in vc++ dll
Thanks,
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,
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,
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,
There¡¯s no way to call managed code from native on NETCF since NETCF does not support hosting.
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
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
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:
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:
//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 callbacks_pCallback (1);
}
Hope this is clear enough
Michael
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
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.
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