Click here to Skip to main content
15,851,453 members
Please Sign up or sign in to vote.
4.33/5 (3 votes)
See more:
I have a managed class library developed in C#.
Now I need to create a win 32 dll wrapper around it.
So in my Win 32 DLL i need to import that class library and call the starting point functions from it.
I am new on this. I was able to create functions and use it in test code.

C++
int _stdcall sum(int x , int y)
{
    return x + y;
}
char* _stdcall to_upper(char *lowerstring)
{
    _strupr(lowerstring);
    return lowerstring;
}


But I need to call external dll (C# class library) and call functions from the functions created in Win32 DLL.
Help would really be appreciated.
Posted
Updated 25-Feb-11 5:09am
v4

There are two ways to do this:


  1. Use C++/CLI to access the C# code. This would mean your native DLL would become mixed mode, which you may not desire. You could split them up though, have a C++/CLI DLL that wraps the C# code and exposes it natively, and your Win32 DLL can access it via the native interface and then you can add whatever extra stuff you want.



  2. Use COM interop to access the C# code from your Win32 DLL. This would mean you'd need to have a decent grasp of using COM, at least from a client perspective.




Word of warning: If you are very new to all this, take a week to get yourself familiar with C++/CLI and/or COM fundamentals before you start on this.

[Edit]
~~~~~~~

In response to your comment asking for a link:

Here's a sample chapter from my C++/CLI book. See section 4.2.1 Accessing a managed library from native code.

http://www.manning.com/sivakumar/sample-ch04_sivakumar.pdf[^]
 
Share this answer
 
v2
Comments
Member 4581741 25-Feb-11 10:32am    
I know C++.
Can u please provide with a link with good reference.

Thanks
Nish Nishant 25-Feb-11 10:36am    
Here's a sample chapter from my C++/CLI book:

http://www.manning.com/sivakumar/sample-ch04_sivakumar.pdf

See section 4.2.1 Accessing a managed library from native code
Member 4581741 25-Feb-11 10:44am    
Thank you for the link. But i need to create Win32 Dll.
So can u provide with a link for COM Interop to access Dll.
Thanks
Nish Nishant 25-Feb-11 10:45am    
Yes, that's what is explained in that section.

If you mean, you don't know how to create a Win32 DLL, then that's a totally different issue. Is that what you are asking here?
Member 4581741 25-Feb-11 11:07am    
No. I already created Win32 Dll and created few functions in that.
Just need to understand how to call external managed DLL and use functions in this unmanaged Win32 Dll.
Thanks
One more problem:

In my class library i have defined char* as parameters.
Now in Win32 Dll after calling that function i am getting error as:

Class Lib:
unsafe int Shk(char* lF, char* sF, int ver)
Win32 Dll:
pSGD->Shk(lF, sF, ver, &lResult)

cannot convert parameter from 'char *' to 'unsigned short *'

Is there a way that i can directly pass string by converting char* -> string.
Or can i pass char* as parameter in calling function.

Help would be appreciated.
 
Share this answer
 
v2
This is another one, radically different approach: direct export from .NET Assembly to unmanaged. Many would say this is not possible, but it's not true: this is allowed by CLR standard and perfectly sage and legitimate. This approach is very lean and, in my opinion, is better then the other two, correctly pointed out by Nishant.

You will find explanation of the idea and good set of references in my other Answer: loading C# DLL in MFC[^]

—SA
 
Share this answer
 
v2
Comments
Nish Nishant 25-Feb-11 21:23pm    
I would very strongly discourage anyone from using this technique which is basically a kind of "hack" unless you are merely experimenting with it. CCW and C++ interop are the proper ways to do this if you expect to have anything resembling good and acceptable design practices.
First : Project --> Add Reference --> COM and select the your c# dll.
Second : in your unmanaged code : using YourC#DLL :)
 
Share this answer
 
Comments
Member 4581741 25-Feb-11 10:30am    
I have created new Win32 Dll project.
Adding reference doesn't show COM.
Please, find 'Add New Reference'
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900