
Signature
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#
> If it's a native app (not managed) then you simply include the header file
> of
> the type you want to reference.
Trying to dllimport native C++ classes is a bad idea which will cause you no
end of trouble down the road. I suggest you rearchitect your application
with a C-compatible interface (with proper memory manager separation), which
is a proven and reliable means of developing DLLs. The object-oriented
version would be COM, and I suggest that if someone gave you a closed source
DLL that "contains a class", it is most likely a COM coclass. To confirm
that, use the oleview SDK tool to check for the presence of a type library.
To use it you load the DLL, GetProcAddress its DllRegisterServer function
and call it, making Windows aware of the coclass, and then use
CoCreateInstance to create an instance of the class, probably using the
com_ptr compiler support.
>> Hi all,
>>
[quoted text clipped - 22 lines]
>> --
>> Samir
Peter Nimmo - 12 Jun 2007 18:06 GMT
> Trying to dllimport native C++ classes is a bad idea which will cause
> you no end of trouble down the road.
What problems are those? The only problems I have found have occured when
I have had a mismatch between run-time library settings between providers
and consumers of classes (i.e trying to free memory allocated by a
different runtime library). Other than that it has been plain sailing.
Obviously the components you write should be all written using the same
version of the compiler, and so if that is not possible then its a bad
idea.
Peter
Ben Voigt [C++ MVP] - 12 Jun 2007 18:12 GMT
>> Trying to dllimport native C++ classes is a bad idea which will cause
>> you no end of trouble down the road.
[quoted text clipped - 7 lines]
> version of the compiler, and so if that is not possible then its a bad
> idea.
If you require all the components to be compiled with the same compiler,
same macros, same settings, what do you gain by having a DLL?
> Peter
Peter Nimmo - 12 Jun 2007 18:33 GMT
>> Obviously the components you write should be all written using the
>> same version of the compiler, and so if that is not possible then its
[quoted text clipped - 5 lines]
>
>> Peter
Drop in replacement without the need to relink the object files/libraries.
If you have designed your interfaces properly then you can change the
implementation of a class, recompile your dll and swap the old one for the
new and bobs your mothers brother.
The components do not need to use the same macros and compiler settings
other than run-time library.
Ben Voigt [C++ MVP] - 13 Jun 2007 18:19 GMT
>>> Obviously the components you write should be all written using the
>>> same version of the compiler, and so if that is not possible then its
[quoted text clipped - 12 lines]
> The components do not need to use the same macros and compiler settings
> other than run-time library.
In that case, you needn't share C++ types with dllimport at all.
__declspec(dllimport) is only used when you are sharing not only interfaces,
but implementation detail, and then you will need to have compiler settings
and macros matching, nor even compiler version.
Unless by interfaces, you mean something other than classes consisting of
pure virtual methods only. In that case, you are in trouble, because the
compiler is placing layout offsets directly into the code on both sides, and
they will not match in case macros or compiler settings aren't the same.
Peter Ritchie [C# MVP] - 12 Jun 2007 18:44 GMT
How can you DllImport a native C++ class in managed code?
> Trying to dllimport native C++ classes is a bad idea which will cause you no
> end of trouble down the road. I suggest you rearchitect your application
[quoted text clipped - 7 lines]
> CoCreateInstance to create an instance of the class, probably using the
> com_ptr compiler support.

Signature
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#
Ben Voigt [C++ MVP] - 13 Jun 2007 18:20 GMT
> How can you DllImport a native C++ class in managed code?
You can't.
I was speaking about __declspec(dllimport), not
System::Runtime::InteropServices::DllImportAttribute
>> Trying to dllimport native C++ classes is a bad idea which will cause you
>> no
[quoted text clipped - 11 lines]
>> CoCreateInstance to create an instance of the class, probably using the
>> com_ptr compiler support.
Peter Ritchie [C# MVP] - 13 Jun 2007 19:00 GMT
Ah, okay. You're comment "Trying to dllimport native C++ classes is a bad
idea..." got me thinking you were talking specifically about importing a
class not functions/methods. A physical impossibility as far as I knew...

Signature
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#
> > How can you DllImport a native C++ class in managed code?
>
[quoted text clipped - 18 lines]
> >> CoCreateInstance to create an instance of the class, probably using the
> >> com_ptr compiler support.
Ben Voigt [C++ MVP] - 14 Jun 2007 06:28 GMT
> Ah, okay. You're comment "Trying to dllimport native C++ classes is a bad
> idea..." got me thinking you were talking specifically about importing a
> class not functions/methods. A physical impossibility as far as I knew...
I was talking about the whole class. Using __declspec(dllimport), from C++
(native) into C++ (native or managed). It is impossible with P/Invoke,
which also uses the token dllimport, but is not at all related.
>> > How can you DllImport a native C++ class in managed code?
>>
[quoted text clipped - 24 lines]
>> >> the
>> >> com_ptr compiler support.