Cannot call method from return value's pointer to interface

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

1
2
3
4
5
6
7
8
9
   
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
    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?
Last edited on
Topic archived. No new replies allowed.