• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 513
  • Last Modified:

Is DLLImport every used with COM?

Is there ever a case when you'd use DLLImport with a COM DLL?  Is that even possible?

COM provides the ability to work with the DLL at an object level.  Whereas DLLImport is a function by function style of interfacing against a Win32 or MFC DLL...correct?
0
brettr
Asked:
brettr
  • 6
  • 4
  • 3
1 Solution
 
kaylanreilorCommented:
Using DLLImport is like using LoadLibrary with a DLL which exposes a kind of catalogue of functions. These function are supposed to static. They are not member of any class.
I would say that you use DLLImport to call native, and then unmanaged, static function exposed by a DLL from a managed module like you use LoadLibrary coupled with GetProcAddress in native C++ world.
Generally people use DLLImport when they want to call and marshall their calling from a managed code to a Win32 API function.
http://msdn.microsoft.com/en-us/library/aa984739(VS.71).aspx

You're right : when we speak about COM technology we speak actually about whole special objects that are, FOR EXAMPLE, exposed from a DLL and that will be instanciated by some client code. Actually, a COM object can also be hosted by an exe file. Moreover, a COM object into a DLL can run into the client process or in a surrogate process like dllhost.
"It is used to enable interprocess communication and dynamic OBJECT creation in a large range of programming languages" => http://en.wikipedia.org/wiki/Component_Object_Model
http://www.codeproject.com/KB/COM/comintro.aspx

I would say that you don't mix DLLImport attribute and COM techno.
If you want to use a COM object from a .Net assembly then you should google around RCW key word.
http://msdn.microsoft.com/en-us/library/8bwh56xe.aspx

As well as with DllImport attribute the main subject here is how to marshall the data between native module and managed code.
0
 
brettrAuthor Commented:
Thanks for the great explanations kaylanreilor.

When you say, "They are not member of any class", how is that possible?  How can a function not be a member of a class in C++?  Or do you refer to a different language?
0
 
kaylanreilorCommented:
They are just global and/or static functions !
A "simple" DLL historically was/is designed to be a kind of server which exposes functions. These function are supposed to do a specific job for your calling code. If you know ASP.Net development, you can make a parallele with Web services for example.
Or, let's examine your native C++ program which needs to retrieves the NetBIOS name of the local computer. Your code will certainly call the Win32 API function "GetComputerName". This function takes a buffer as out parameter and populate it with the info. This function is exposed by the Kernel32.dll.
A kind of DLL I'm talking about will be a server of a list of function and will have an entry point like
BOOL WINAPI DllMain( __in  HINSTANCE hinstDLL, __in  DWORD fdwReason, __in  LPVOID lpvReserved);
If you wanted to write such a DLL you would implement it. This function will be called by the system when your calling will do a LoadLibrary on your DLL. Then you would have written a myLib.def file like the following:
LIBRARY      "myLib"
EXPORTS
      f1

And then you would have define the interface into a myLib.h file:
#ifndef __MYLIB_H__
#define __MYLIB_H__
 
#ifdef __cplusplus
	extern "C" {
#endif
 
typedef struct _myStruct
{
	DWORD type;
	ULONG count;
} myStruct;
 
 
myStruct __stdcall f1(LPCWSTR key);
 
#ifdef __cplusplus
};
#endif
 
 
#endif //  __MYLIB_H__

Open in new window

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
kaylanreilorCommented:
Now, you could also have developped a COM object into a DLL that would have exposed the following interface:
; myComLib.def : Declares the module parameters.
LIBRARY      "myComLib.DLL"
EXPORTS
      DllCanUnloadNow     @1 PRIVATE
      DllGetClassObject   @2 PRIVATE
      DllRegisterServer   @3 PRIVATE
      DllUnregisterServer @4 PRIVATE

Then DllGetClassObject would have return an interface on the COM object. But in such a casae I doubt you would try to use DllImport... or if you really try to do it, you will have to pollute your COM in such a way that it won't be a COM anymore.

In which context are you ? Are you working on native C++ project only ?
0
 
brettrAuthor Commented:
Thanks for the great explanations.

>>In which context are you ? Are you working on native C++ project only ?<<

Not at this time.  It's a .NET project.  I haven't written any COM libraries and was curious as to what happen when you try to use DLLImport with a COM library.

For the f1 function you've defined above, you'll also define the implementation somewhere correct?  It's throwing me off because I'm not familiar with a function that is not a member of a class.  Is there an MSDN reference to how that works?
0
 
brettrAuthor Commented:
>>They are not member of any class<<

Because Win32 is C right?  I was thinking object oriented.
0
 
kaylanreilorCommented:
There are some stuffs here:
http://support.microsoft.com/kb/815065

Of course f1 above is implemented in a cpp file.

I don't think you can use DLLImport with a COM library because the first definition of a real COM is "a class which inherits at least from IUnknown interface"... Now, if a vendor provides a DLL which provides a so-called COM with some global functions...
0
 
brettrAuthor Commented:
>> Now, if a vendor provides a DLL which provides a so-called COM with some global functions...<<

How is that possible.  Object oriented languages such as C++ won't allow a global function.  It has to be in a class and referenced through the class.  If a global function, then it would need to be written in C.   Unless you are suggesting a language other than C or C++.
0
 
Jens FiedererTest Developer/ValidatorCommented:
You might be thinking of Java.
C++ most certainly DOES allow global functions, just like C.
0
 
Jens FiedererTest Developer/ValidatorCommented:
You probably aren't used to seeing them very often because of style considerations.  Of course, unless you are programming in some development environment that generates it for you, the big exception is the global "main" function!

See, for example, http://www.horstmann.com/ccj2/ccjapp3.html

0
 
brettrAuthor Commented:
>>C++ most certainly DOES allow global functions, just like C.<<

Ah, thanks.  Besides main, what's an example where you'd prefer a C++ global function vs class member?
0
 
Jens FiedererTest Developer/ValidatorCommented:
I can actually think of only one, which is probably why you thought it didn't happen: the main use of global functions is linkage to old "C" code, and while those functions are called as global functions in your C++ code, their actual CODE is in C.

0
 
brettrAuthor Commented:
Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now