LPVARIANT param returned from unmanaged dll (.net Interop)

E

evadell

Hi all. I have a c# windows forms app that uses a unmanaged dll. I need
to call a function in the dll that registers a callback function. When
the dll generates an event it calls my callback function. The problem
is that when my callback function is called a LPVARIANT value is
returned. Depending on another param, it can be a string or a int. If
all I receive was int, there were no problems, but I don't know how to
handle when i receive a string.

Here is the dll function definition:


DWORD MFPSB_API MFPSB_RegisterCallback(MFPSB_HCTX hCtx,
MFPSB_PFNCALLBACK pfnCallback, DWORD dwEvents, HWND hwnd);

typedef VOID (MFPSB_API *MFPSB_PFNCALLBACK)(MFPSB_HCTX hCtx, LONG lpId,
LPVARIANT lpInfo);

And here my c# app code:

delegate void DMFPSB_PfnCallback(int hCtx,uint lpId,ref int lpInfo);

[DllImport("mfpsb.dll")]
private static extern int MFPSB_RegisterCallback(int hCtx,
DMFPSB_PfnCallback pfnCallback, uint dwEvents, int hwnd);

result = MFPSB_RegisterCallback(sc.Handler,new
DMFPSB_PfnCallback(this.Callback),MFPSB_CB_ALLEVENTS,0);

public void Callback(int hCtx, uint lpId, ref int lpInfo) {
switch (lpId) {
case MFPSB_CB_NEWFILE:
//Here lpInfo has to be a String
Console.WriteLine("Callback " + hCtx + " MFPSB_CB_NEWFILE " +
lpInfo);
break;
case MFPSB_CB_ERROR:
//Here lpInfo has to be a int
Console.WriteLine("Callback " + hCtx + " MFPSB_CB_ERROR" + lpInfo);
break;
}
return;
}

Thnx.
 
C

chpichaud

You can handle the type of the VARIANT using the vt value.
Definition of VARIANT is described in OAIDL.H automation header file.
Check the vt value.
if vt == VT_BSTR then take the bstrVal value.
Maybe sure it is not a ByRef.

BSTR bstrVal; // VT_BSTR.
BSTR * pbstrVal; // VT_BYREF|VT_BSTR.


It should be easier to use your unmanaged library in a Managed C++
project.
Because making (the old way) a cast like: if( variant.vt == VT_xx) then
OLE2T LPTSTR lpszToto = (LPTSTR)
You can mix managed and unmanaged code in the same project.

My 0.02 cent
Christophe Pichaud.
 

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