By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,198 Members | 1,412 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,198 IT Pros & Developers. It's quick & easy.

LPVARIANT param returned from unmanaged dll (.net Interop)

P: n/a
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_ALLEVEN TS,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.

Mar 28 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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.

Mar 28 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.