InvokeMember always returns 0!

  • Thread starter Thread starter JB
  • Start date Start date
J

JB

I have a function in a DLL which displays a message and sets a return
value;

//Function in DLL
function ShowMsg(pMsg : PChar) : Word; stdcall;
var
Msg : string;
res : word;
sres : string;
begin
Msg := StrPas(pMsg);
self.Edit1.text := msg;
self.ShowModal; // Show my message
res := 137;
sres := IntToStr(res);
ShowMessage(sres); // Show my function result value
result := res;
end;

and I call that function from VB.net as;

'Call function in DLL
Dim target As Object = Activator.CreateInstance(t)
res = t.InvokeMember("ShowMsg", BindingFlags.InvokeMethod Or
BindingFlags.Static Or BindingFlags.Public, Nothing, target, args)
MsgBox(res) ' Show my function result value

Now whenever I run that function it calls the DLL displays the
message, it then displays the result value (137 in this example), it
then returns to vb.net and displays the function result value as 0.
It does this even if I explicitly cast the result to an integer or
int16 value.

Can anyone explain to me why this does not get the return value out of
the DLL?

TIA
 
JB said:
I have a function in a DLL which displays a message and sets a return
value;

//Function in DLL
function ShowMsg(pMsg : PChar) : Word; stdcall;
var
Msg : string;
res : word;
sres : string;
begin
Msg := StrPas(pMsg);
self.Edit1.text := msg;
self.ShowModal; // Show my message
res := 137;
sres := IntToStr(res);
ShowMessage(sres); // Show my function result value
result := res;
end;

Is this a COM or .NET class/function?
 
JB said:
I have a function in a DLL which displays a message and sets a return
value;

//Function in DLL
function ShowMsg(pMsg : PChar) : Word; stdcall;
var
Msg : string;
res : word;
sres : string;
begin
Msg := StrPas(pMsg);
self.Edit1.text := msg;
self.ShowModal; // Show my message
res := 137;
sres := IntToStr(res);
ShowMessage(sres); // Show my function result value
result := res;
end;

and I call that function from VB.net as;

'Call function in DLL
Dim target As Object = Activator.CreateInstance(t)

I doubt that this will work because you don't have a valid type object in
this scenario. I am curious why you don't use 'Declare' or 'DllImport' to
declare the function and then call it directly. Alternatively you may find
the technique described in the article and sample at
<URL:http://www.msjogren.net/dotnet/eng/samples/dotnet_dynpinvoke.asp>
useful.
 
I doubt that this will work because you don't have a valid type object in
this scenario. I am curious why you don't use 'Declare' or 'DllImport' to
declare the function and then call it directly. Alternatively you may find
the technique described in the article and sample at
<URL:http://www.msjogren.net/dotnet/eng/samples/dotnet_dynpinvoke.asp>
useful.

I don't use DllImport or Declare because to do that I have to
explicitly name the DLL in my source code and, in this application,
although I know the function protoype exactly I do not know the DLL.

The code I am using is based on Mattias' example although it has moved
on somewhat from the original, in fact in his sample he uses;

Dim hr As Integer
hr = CInt(m_tDllReg.InvokeMember(sMemberName,
BindingFlags.InvokeMethod, Nothing, _
Activator.CreateInstance(m_tDllReg), Nothing))
If hr <> 0 Then Marshal.ThrowExceptionForHR(hr)

while I use;

'Call function in DLL
Dim target As Object = Activator.CreateInstance(t)
res = t.InvokeMember("ShowMsg", BindingFlags.InvokeMethod Or
BindingFlags.Static Or BindingFlags.Public, Nothing, target, args)
MsgBox(res) ' Show my function result value

So apart from the explicit casting in Mattias' example (which I have
tried) I can not see where the difference lies and yet every test
returns 0!
 

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

Back
Top