Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / .NET SDK / August 2004

Tip: Looking for answers? Try searching our database.

how to call C++ lib file from C#

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
sunny - 06 Jul 2004 03:53 GMT
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
Klaus H. Probst - 07 Jul 2004 20:06 GMT
Nope. It implies the ability to link statically, and .NET won't do that.

Signature

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

> Dear everyone:
>     I know I can call a dll by c# like this:
[quoted text clipped - 9 lines]
>
> Tom
Richard Grimes [MVP] - 02 Aug 2004 20:14 GMT
> Dear everyone:
>     I know I can call a dll by c# like this:
[quoted text clipped - 7 lines]
> "a.lib". Could anybody tell me how to do it?
>     Thanks!

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
Signature

my email evpuneqt@zicf.bet is encrypted with ROT13 (www.rot13.org)
sign up for my free .NET newsletter at
http://www.wd-mag.com/newsletters/

alanrn - 03 Aug 2004 01:21 GMT
So, how is option #1 accomplished?

.ARN.

> > Dear everyone:
> >     I know I can call a dll by c# like this:
[quoted text clipped - 20 lines]
>
> Richard
Richard Grimes [MVP] - 08 Aug 2004 17:37 GMT
>> 1) use unmanaged C++ to expose the lib functions as exported
>> (unmanaged) DLL functions
>
> So, how is option #1 accomplished?

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
Signature

my email evpuneqt@zicf.bet is encrypted with ROT13 (www.rot13.org)
sign up for my free .NET newsletter at
http://www.wd-mag.com/newsletters/


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.