1

I building Wrapper in C++/CLI for C Static library to be used in .NET application through C#

I have function like this in C

long    My_COM_Interface(   PVOID hDevice,IUnknown **pUnknown);

How to declare IUnknown ** in C++/CLI

for first argument I am using IntPtr but Not able to find out the Replacement for IUknown.

I have another COM Data type GUID in another function this is also an issue for me.

Please Help me find the relacement for data type IUnknown and GUID.

ildjarn
  • 62,044
  • 9
  • 127
  • 211
Vikram Ranabhatt
  • 7,268
  • 15
  • 70
  • 133

3 Answers3

1

There is no replacement.

C++/CLI understands native types just fine. Include the right header files, and you can use IUnknown* like always.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • Actually, you should not, at least not on public methods. If you do you will have to use pointers on any other .net assembly that uses your library, which is not ideal. – yms Dec 12 '13 at 18:02
  • @yms: The C++/CLI class should completely wrap the native object, so that other assemblies using your library never even know that there is an `IUnknown*`, nevermind have to handle it. – Ben Voigt Dec 12 '13 at 18:12
  • But the question was how to **declare** a method that uses IUnknown, so the answer cannot be to just use IUnknown... – yms Dec 12 '13 at 18:16
  • @yms: But it's related to *consuming* the C code, not related to building the managed interface for use by other .NET code. – Ben Voigt Dec 12 '13 at 18:18
  • @yms: Have you ever built such a wrapper? I have, many times. Inside the implementation you use all the usual native pointer types. The public interface to the wrapper, which .NET consumers use, are the `ref class` types defined in C++/CLI. The consumer doesn't see the native pointers, only a set of managed methods (available behaviors, which internally use pointers and native library calls and Win32 APIs, etc). – Ben Voigt Dec 12 '13 at 18:42
  • Yes, I have built such wrappers, I would not be discussing this with you otherwise. My point was that from the description of the question it is not clear if the OP was trying to use IUnknown* internally or in method declaration that he intended to use from .Net, and your answer does not make the distinction either. – yms Dec 12 '13 at 19:54
  • @yms: From the question it is clear that `IUnknown**` appears on the "C static library" side of the wrapper. – Ben Voigt Dec 12 '13 at 19:58
  • @yms: Specifically, I think that your second comment "the question was how to declare a method" was wrong. The question says nothing about declaring a method. He asked how to declare a variable, in the context of how to call an existing function with that kind of parameter. – Ben Voigt Dec 12 '13 at 19:59
0

I'd refer to the APIs, e.g.

 public static IntPtr GetIUnknownForObject(Object o);

This API can simply be used from C++/CLR and suggests you should use IntPtr^

sehe
  • 374,641
  • 47
  • 450
  • 633
-1

try using parameter like;

ref object pUnknown

and use it like

MyObject o = pUnknown as MyObject
Emir Akaydın
  • 5,708
  • 1
  • 29
  • 57