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 / Languages / Managed C++ / June 2007

Tip: Looking for answers? Try searching our database.

Refering to Class, Types, and Functions from Dlls

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
safaci2000@gmail.com - 25 May 2007 22:15 GMT
Hi all,

  I have a DLL that I need to access.  I've managed to load it and
unload it successfully.  The compiler is happy no complaints or
warning.

I need to refer to a class object that was defined in the class, and
I'm not sure how to go about it.

I understand that if I need to call a function I'd need to call
GetProcAddress(hDll,"funcFoo"); that would return a pointer to the
function and then I'd use the pointer to address the function inside
of my program.

what would I need to do to refer to the Dll, types.

If I was in a regular C++ program, I would be doing something like

ClassFoo.Host  myhost;  // assuming Host if an internal class, is
public and is well defined.

How would I be able to do that if my class Foo is defined inside the
dll?

--
Samir
Peter Ritchie [C# MVP] - 27 May 2007 16:21 GMT
If it's a native app (not managed) then you simply include the header file of
the type you want to reference.

Signature

Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#

> Hi all,
>
[quoted text clipped - 22 lines]
> --
> Samir
Ben Voigt - 28 May 2007 15:30 GMT
> 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.

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.