Using a C Library in a managed DLL

  • Thread starter Thread starter Kidan
  • Start date Start date
K

Kidan

I have a .LIB file, built an compiled in C that I need to use in
my .NET application.

If I build a C++.Net Console application and activate the exposed
functions directly the library works perfectly.

If I try to compile the .LIB file into a managed DLL for use in either
my VB.Net GUI or my managed C++.Net Console application I get a Null
Reference Exception when the LIB makes a MALLOC call.

Is there some command or option that I need to set to allow my managed
DLL to be able to run MALLOC?

Thanks!
 
Kidan said:
I have a .LIB file, built an compiled in C that I need to use in
my .NET application.

If I build a C++.Net Console application and activate the exposed
functions directly the library works perfectly.

If I try to compile the .LIB file into a managed DLL for use in either
my VB.Net GUI or my managed C++.Net Console application I get a Null
Reference Exception when the LIB makes a MALLOC call.

What you can't do is call malloc in the DLL and free in the C++ console
application. Otherwise what you describe should work. Double-check the
argument to malloc and make sure it's reasonable, perhaps you are trying to
allocate sizeof(int) * -1 or something.
Is there some command or option that I need to set to allow my managed
DLL to be able to run MALLOC?

No, not really. Although operator new is preferred over malloc for C++
programs.
 
What you can't do is call malloc in the DLL and free in the C++ console
application. Otherwise what you describe should work. Double-check the
argument to malloc and make sure it's reasonable, perhaps you are trying to
allocate sizeof(int) * -1 or something.
The malloc call is within the precompiled .LIB file so I don't have
access to it's calls/arguments.

No, not really. Although operator new is preferred over malloc for C++
programs.
Yeah, I prefer new, but unfortunately the precompiled library is in C.
 
Kidan said:
The malloc call is within the precompiled .LIB file so I don't have
access to it's calls/arguments.

Then how do you know that malloc is failing?

You could also provide your own malloc/free implementations as a thin
wrapper around HeapAlloc, with some extra logging.
 
Then how do you know that malloc is failing?

You could also provide your own malloc/free implementations as a thin
wrapper around HeapAlloc, with some extra logging.

Because when I get the null reference exception, the C++ debugger
takes me to a malloc call (the VB.Net debugger just took me to
the .LIB call).
 
Back
Top