Cannot call method from return value's pointer to interface

  • Thread starter Thread starter HiddenHandX
  • Start date Start date
H

HiddenHandX

Guest
I am getting error from a method call when I try to access the functions of an interface.



My_DLL::IStubRequest *request = NULL;
CoCreateInstance(__uuidof(My_DLL::StubRequest), NULL, CLSCTX_INPROC_SERVER, __uuidof(My_DLL::IStubRequest), (void**)&request); //this is ok.
My_DLL::IStubResponse *response = NULL;
request->DoSomething((My_DLL::_StubResponse**) &response); //HRESULT is ok, but using it on a method without return value will cause System.AccessViolationException

response->Print();//System.AccessViolationException
BSTR * valuePtr = NULL;
response->GetValue(valuePtr); //this is not ok


The Stub* classes in C# are as follows:


namespace com.myApp.msg
{

public interface IStubRequest
{
StubResponse DoSomething();
}

public class StubRequest : IStubRequest
{
public StubRequest()
{
}
public StubResponse DoSomething()
{
//do something and return StubResponse
}
}

public interface IStubResponse
{
string GetValue();

void Print();
}

public class StubResponse : IStubResponse
{
private string value = null;
public StubResponse(string value)
{
this.value = value;
}
public string GetValue()
{
return value;
}

public void Print(){

Console.WriteLine("hello world");

}
}
}




The GetValue() call results in a popup window saying

>Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function declared with a different calling convention.

How to fix it?

Continue reading...
 
Back
Top