David N.4117

Hi All,

I inheritted quite a few static libraries code developed at the time when MFC was the "state of the art". After many..years maintained by previous developers, these libraries now still can be compiled with VC++ 2003. However, making these existing library code compiled with VC++ 2005 is truly a pain.

I am thinking about converting these existing, old library code into DLL which can be compiled/linked with VC++ 2005. My questions are:

1. Is it possible to convert these old libraries to DLL

2. If it is possible, what do I have to do

3. Is there a forum/article on the Internet described the works that have to be done to convert a static lib to DLL If there is, can someone direct me to the url

Thanks in advance.




Re: Visual C++ General Convert Static Lib to DLL

nobugz

I'm not sure a DLL is going to solve your problem, you'll still have to compile the library.

Anyhoo, the generic solution is to change the header file declarations of the classes and functions in your library that you want to be available in the DLL to something like this:
#undef EXPORT
#ifdef _USRDLL
#define EXPORT __declspec(dllexport)
#else
#define EXPORT
#endif

class EXPORT MyClass {}
EXPORT void MyFunc();
...etc

Now just include those header files in your DLL's main program, add the .lib to your Additional depencies and build.





Re: Visual C++ General Convert Static Lib to DLL

David N.

Thanks nobugz,

You are right. Convert it to a DLL this way would solve the problem. I still cannot use the DLL in my .NET code.

So the question really is, how to utilize, or how to reuse the static library code with .Net Framework






Re: Visual C++ General Convert Static Lib to DLL

nobugz

That's a whole different ballgame. You'll have to write a wrapper around the library code and need to use managed C++ (__gc class) or C++/CLI (ref class) classes...





Re: Visual C++ General Convert Static Lib to DLL

David N.

Can you direct me to a webpage/article that has information regarding how to write a wrapper I have been searching unsuccessfully.

Many Thanks






Re: Visual C++ General Convert Static Lib to DLL

HDjim

use this statement in c#

[DllImport("your.dll")]

public static extern string yourFunction();

then call yourFunction() as usual!

That's it!

Cheers!






Re: Visual C++ General Convert Static Lib to DLL

Bruno van Dooren

That won't work because P/Invoke only works with C style functions. You cannot import native classes.

The only way to get that to work is to create a managed wrapper class that simple delegates to an instance of the native class.

The wrapper class also needs to convert from native parameter types to managed parameter types and vice versa.





Re: Visual C++ General Convert Static Lib to DLL

HDjim

We are talking about C++ to C# so yes you do have to change the data types to C# datetypes. I left that out because thiose are subtle details that you already should have known.

This example I gave is a simple example and works for most functions. Don't get confused... this is the IMPORT portion that is the wrapper for C#.

The export portion simply takes the prototypes (C++) and exports them as standard C functions (no mangling - overloading). That's why the "C" is present in the export statement:

export "C" __declspec (dllexport) <your prototype goes here>;

I do this quite often and this works fine for me.

Take whatever you can get out of this and do not tell me that this does NOT work when IT DOES.






Re: Visual C++ General Convert Static Lib to DLL

Bruno van Dooren

HDjim wrote:

Take whatever you can get out of this and do not tell me that this does NOT work when IT DOES.

Except it doesn't when we are talking about C++ classes, not simple exported functions.

nobugz's example showed how to export a class as well as a function, but for classes that approach will not work

For simple C style functions (regardless of whether they are compiled with C or C++) your example works fine.

But an exported C++ class cannot simply be imported by DllImport. If you have any exmple to the contrary I'd be glad to see it but afaik there is no practical way to do it.