DLL-Function with Variant used in C#

G

Guest

Hello everybody,

I have a Problem with C Sharp. I have to use external functions in DLLs.
This Functions are written in Visual C++ 6 like this:

int (__stdcall *GetGlobalValue) (VARIANT Appl, VARIANT &vRetVal);

Now for the first time I want to use C Sharp and I find some examples to use
Functions in extarnal DLLs, but I don't know how to cast the
Variant-Paramters in C Sharp.
I tried it out with the datatype "Object" in C Sharp, but it dosen't work.

Thank you very much for any solution!


[DllImport("...test.dll", EntryPoint = "GetGlobalValue")]
private static extern int GetGlobalValue(Object Para, Object RetVal);
public int CsGetGlobalValue(Object argPara, Object argRetVal)
{
if (GetGlobalValue(argPara, argRetVal) != 0)
{
return 1;
}
else
{
return -1;
}
}
 
S

Stoitcho Goutsev \(100\)

Heike,

Look at the Marshal class and more specifically at
Marshal.GetObjectForNativeVariant and Marshal.GetNativeVariantForObject.

BTW any time you need to deal with PInvoke and COM Interop first take a look
at the Marshal class.
 
N

Nicholas Paldino [.NET/C# MVP]

It actually doesn't matter. This function can not be accessed since it
is passing a reference to the variant (the & indicates a reference in C++).

The P/Invoke layer/COM interop does not allow references. Rather, you
have to declare the parameter as a pointer.

Additionally, the return value is a pointer, not an int, so you have to
return an IntPtr and then get the value pointed to.

So, first the declaration of the function exported has to be changed to:

int (__stdcall *GetGlobalValue) (VARIANT Appl, VARIANT *vRetVal);

Then, the declaration in C# is:

[DllImport("...test.dll", EntryPoint = "GetGlobalValue")]
private static extern int GetGlobalValue(Object Para, ref Object RetVal);

To get the actual int returned from the function, you need to do this:

// The pointer returned from GetGlobalValue.
IntPtr globalValuePointer = GetGlobalValue(argPara, argRetVal);

// Convert.
int globalValue = Marshal.ReadInt32(globalValuePointer);

// Check the value against zero.
if (globalValue != 0)
{
return 1;
}
else
{
return -1;
}

There is still one more problem. Because GetGlobalValue returns a
pointer, it means that it is allocating memory inside the function. You
need to deallocate that memory when done. How you do it depends on how it
is allocated (which you need the code to figure out, or whatever is
specified in the documentation if you don't have the code).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Stoitcho Goutsev (100) said:
Heike,

Look at the Marshal class and more specifically at
Marshal.GetObjectForNativeVariant and Marshal.GetNativeVariantForObject.

BTW any time you need to deal with PInvoke and COM Interop first take a
look at the Marshal class.


--
HTH
Stoitcho Goutsev (100)


Heike said:
Hello everybody,

I have a Problem with C Sharp. I have to use external functions in DLLs.
This Functions are written in Visual C++ 6 like this:

int (__stdcall *GetGlobalValue) (VARIANT Appl, VARIANT &vRetVal);

Now for the first time I want to use C Sharp and I find some examples to
use
Functions in extarnal DLLs, but I don't know how to cast the
Variant-Paramters in C Sharp.
I tried it out with the datatype "Object" in C Sharp, but it dosen't
work.

Thank you very much for any solution!


[DllImport("...test.dll", EntryPoint = "GetGlobalValue")]
private static extern int GetGlobalValue(Object Para, Object
RetVal);
public int CsGetGlobalValue(Object argPara, Object argRetVal)
{
if (GetGlobalValue(argPara, argRetVal) != 0)
{
return 1;
}
else
{
return -1;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top