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.

int _stdcall sum(int x , int y)
    return x + y;
char* _stdcall to_upper(char *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.
Updated 25-Feb-11 5:09am

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.


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.[^]
Share this answer
Member 4581741 25-Feb-11 10:32am    
I know C++.
Can u please provide with a link with good reference.

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

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.
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.
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
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[^]

Share this answer
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
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