Click here to Skip to main content
15,894,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a DLL written in C++, now I want to invoke the DLL in ASP.NET Web Application. How should I do?
The environment is Visual Studio 2008, C#.
Thank you!
Posted

It's called P/Invoke and uses System.Runtime.InteropServices and System.Runtime.InteropServices.DllImportAttribute.
You can find a simple code sample here: http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute.aspx[^].

It may me fairly simple or quite tricky. To specify marshaling between native (unmanaged) and managed parameters you may also need to use the attribute System.Runtime.InteropServices.MarshalAsAttribute, see the code sample:
http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshalasattribute.aspx[^].

In more complex cases (in particular, when memory allocation is involved), you may need to use explicit marshaling methods with the class System.Runtime.InteropServices.Marshal, see http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.aspx[^].

In even more complex cases you may need to use custom marshaling. See, for example: http://msdn.microsoft.com/en-us/magazine/cc164085.aspx[^].

[EDIT]

After changes in Microsoft documentation, most adequate link to P/Invoke and C++/CLI implicit P/Invoke is this:
https://msdn.microsoft.com/en-us/library/ms235282.aspx[^].

—SA
 
Share this answer
 
v2
Comments
flyicing 9-Jun-11 20:09pm    
Thank you for you details!
But now I encounter an error: cannot find the DLL, though I have put it under /bin folder. Bye the way, I also try to put it under Windows/systems32, but still error.
Sergey Alexandrovich Kryukov 10-Jun-11 11:39am    
OK, ***you*** cannot find DLL and think this is because my answer is not good enough, or something?
And did you voted 1 by this reason?
--SA
flyicing 12-Jun-11 9:12am    
Oh,my god! I am really sorry. I just don't know what that vote mean. Your answer is perfect, with so many details. I am sincerely thank you! Please forgive my fault! I am so Sorry!
Sergey Alexandrovich Kryukov 12-Jun-11 13:46pm    
No problem at all, mistakes happen; admitting them and decent respond to criticism is the virtue! Is you still have a problem, feel free to ask a follow-up questions, as people usually do.
Thanks for accepting this, answer, hope it helps.

Good luck, call again.
--SA
Maciej Los 9-Apr-15 15:01pm    
Sergey, why do you made some changes to this answer?
Hello,
You can call in .net through api

GetProcAddress
and
Marshal.GetDelegateForFunctionPointer using this you can pass the parameter to the c++ function
 
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