无法从返回值的指针调用接口的方法

时间:2018-06-29 06:43:48

标签: visual-c++

当我尝试访问接口的功能时,方法调用出现错误。

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

C#中的Stub *类如下:

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");
        }
    }
}

GetValue()调用会在弹出窗口中显示

  

运行时检查失败#0-在整个函数调用中ESP的值未正确保存。通常,这是由于用一种调用约定声明的函数与用另一种调用约定声明的函数进行调用的结果。

如何解决?

0 个答案:

没有答案