.NETGURU
how to call C++ lib file from C#
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.sdk.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...

sunny
Dear everyone:
I know I can call a dll by c# like this:

using System.Runtime.InteropServices;
[DllImport("user32.dll")]
public static extern int MessageBox(int hWnd, String text,
String caption, uint type);

but now I want to call a lib file created by VC6.0,for example "a.lib".
Could anybody tell me how to do it?
Thanks!

Tom

Reply to this message...
 
    
Richard Grimes [MVP]
sunny wrote:
[Original message clipped]

you have two options:

1) use unmanaged C++ to expose the lib functions as exported (unmanaged) DLL
functions
2) use managed C++ to expose a managed class

Managed C++ can use unmanaged .lib files, which is why #2 works, however,
there are issues with mixing managed and unmanaged code in a library
assembly (caused by the Win32 DLL loader holding various locks which could
cuase a deadlock). #1 is the safest option.

Richard
--
my email Click here to reveal e-mail address is encrypted with ROT13 (www.rot13.org)
sign up for my free .NET newsletter at
http://www.wd-mag.com/newsletters/

Reply to this message...
 
    
alanrn (VIP)
So, how is option #1 accomplished?

..ARN.

"Richard Grimes [MVP]" wrote:

[Original message clipped]

Reply to this message...
 
    
Richard Grimes [MVP]
alanrn wrote:
[Original message clipped]

Well, if you have a lib that provides this function:

bool test_func(int param1, char* param2);

you create a DLL with this function:

extern "C" __declspec(dllexport) bool Test_Func(int param1, char* param2)
{
return test_func(param1, param2);
}

Richard
--
my email Click here to reveal e-mail address is encrypted with ROT13 (www.rot13.org)
sign up for my free .NET newsletter at
http://www.wd-mag.com/newsletters/

Reply to this message...
 
    
Klaus H. Probst
Nope. It implies the ability to link statically, and .NET won't do that.

--
Klaus H. Probst, MVP
http://www.vbbox.com/

"sunny" <Click here to reveal e-mail address> wrote in message
news:#Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
 
System.Windows.Forms.MessageBox




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification