Call to WCF-service from unmanaged c++ causes access violation

1.5k Views Asked by At

I want to access a wcf-service from unmanaged code. The service just concats to strings and returns them, to keep things simple.

public class Stringconcat : IStringconcat
{
        public string stringconcat(string a, string b)
        {
            return b + a;
        }
}

[ServiceContract]
public interface IStringconcat
{
    [OperationContract]
    string stringconcat(string a, string b);
}

I host that service, with a c# console app that references the .dll of the service. And exposes the endpoint for the service. In the Main():

using (ServiceHost host = new ServiceHost(typeof(stringconcatservice.Stringconcat)))
{
    host.Open();
    Console.WriteLine("Press <ENTER> to terminate the host application");
    Console.ReadLine();
}

The config of the host looks like this:

<?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <system.serviceModel>
            <behaviors>
                <serviceBehaviors>
                    <behavior name="serviceBehavior">
                        <serviceDebug includeExceptionDetailInFaults="true"/>
                        <serviceMetadata />
                    </behavior>
                </serviceBehaviors>
            </behaviors>
            <services>
                <service behaviorConfiguration="serviceBehavior"
                name="stringconcatservice.Stringconcat" >
                    <endpoint address="stringconcat" binding="basicHttpBinding"
                    name="basicHttp" contract="stringconcatservice.IStringconcat" />
                    <endpoint binding="mexHttpBinding" name="mex"
                    contract="IMetadataExchange" />
                    <host>
                        <baseAddresses>
                         <add baseAddress="http://localhost:8000/stringconcatService" />
                        </baseAddresses>
                    </host>
                </service>
            </services>
        </system.serviceModel>
    </configuration>

Now everything works if I call the service from a .net client that uses for the service reference the base address.

StringconcatClient proxy = new StringconcatClient();

string a = " string a ";
string b = " string b ";
string c = proxy.stringconcat(a, b);

Console.WriteLine(c);
Console.WriteLine("Press <ENTER> to terminate Client.");
Console.ReadKey();

Now to the problem I have. Because I want to access the service from unmanaged c++ code. For that i use a .dll, that registers for com interop and also uses the service reference of the running host.

[Guid("E4435D3B-CBC3-4D41-B4A5-D8116B394195")]
[ClassInterface(ClassInterfaceType.None)]
[ComVisible(true)]
public class Stringconcat_IAMCOM : IStringconcat_IAMCOM
{
    public string stringconcat(string a, string b)
    {
        StringconcatClient client = new StringconcatClient();
        return client.stringconcat(a, b);
    }
}
[Guid("E130F97E-1844-4C8B-9E57-AF42632A2557")]
[ComVisible(true)]
public interface IStringconcat_IAMCOM
{
    [DispId(1)]
    string stringconcat(string a, string b);
}

Now when i try to access the service over the intermediary .dll whith c++...

#import "..\stringconcatInteropSvcClient\bin\Debug\stringconcatInteropSvcClient.tlb" 
#include <iostream>
#include <stdio.h>
using namespace stringconcatInteropSvcClient;
int main(int argc, const char* argv[])
{
    HRESULT hr = CoInitialize(NULL);
    BSTR a =  L" string a ";
    BSTR b =  L" string b ";
    IStringconcat_IAMCOM* q;
    hr = CoCreateInstance(__uuidof(Stringconcat_IAMCOM), NULL, CLSCTX_INPROC_SERVER,
    __uuidof(IStringconcat_IAMCOM), (void**)&q );
    std::cout << q->stringconcat(a, b);

    //Uninitialize COM.
    CoUninitialize();
    return 0;
}

... I get an exception in stringconcatinteropsvcclient.tli like:

"Unhandeled exception at 0x75... in xyz.exe: Microsoft C++ exception: _com_error at memory location 0x..."

then I can continue and get the next exception in strlen.asm:

"Unhandeled exception at 0x5d... (msvcr100d.dll) in xyz.exe: 0xC0...: Access violation reading location 0x00..."

after this exception stops. Maybe somebody has an solution for this problem or an idea where the error could be. I just use this example to keep it simple. The project I am working on uses the same structure. It also uses wcf services and these will be called from unmanaged c++.

2

There are 2 best solutions below

0
On

Just created small test project, it is working:

namespace ComTest
{
    [Guid("E4435D3B-CBC3-4D41-B4A5-D8116B394195")]
    [ClassInterface(ClassInterfaceType.None)]
    [ComVisible(true)]
    public class Stringconcat_IAMCOM : IStringconcat_IAMCOM
    {
        public string stringconcat(string a, string b)
        {            
            return a + b;
        }
    }
    [Guid("E130F97E-1844-4C8B-9E57-AF42632A2557")]
    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface IStringconcat_IAMCOM
    {
        [DispId(1)]
        string stringconcat(string a, string b);
    }
}

And client:

#import "C:\Users\rst\Documents\Visual Studio 2008\Projects\ComTest\ComTest\bin\Debug\ComTest.tlb" 
#include <iostream>
#include <stdio.h>
#include <windows.h>

using namespace ComTest;

    int main(int argc, const char* argv[])
    {

        HRESULT hr = CoInitialize(NULL);
        BSTR a =  L" string a ";
        BSTR b =  L" string b ";
        IStringconcat_IAMCOM* q;
        hr = CoCreateInstance(__uuidof(Stringconcat_IAMCOM), NULL, CLSCTX_INPROC_SERVER,
        __uuidof(IStringconcat_IAMCOM), (void**)&q );
        std::cout << q->stringconcat(a, b);

        //Uninitialize COM.
        CoUninitialize();
        return 0;
    }

My assembly name is ComTest.dll so i registered it using:

regasm Comtest.dll /CodeBase
0
On

I had an error in the c++ code, false call of CoCreateInstance(...), thank you zilog for the help.

IStringconcat_IAMCOM* q;
hr = CoCreateInstance(__uuidof(Stringconcat_IAMCOM), NULL, CLSCTX_INPROC_SERVER, __uuidof(IStringconcat_IAMCOM), (void**)&q );

And there was also an error in the the call for the serviceproxy. I had to add to the base address the endpoint attribute address="stringconcat". The address should have been like this:

http://localhost:8000/stringconcatService/stringconcat

And I create the service proxy now this way:

[Guid("E4435D3B-CBC3-4D41-B4A5-D8116B394195")]
[ClassInterface(ClassInterfaceType.None)]
[ComVisible(true)]
public class Stringconcat_IAMCOM : IStringconcat_IAMCOM
{
    public string stringconcat(string a, string b)
    {  
        EndpointAddress ep = new EndpointAddress("http://localhost:8000/stringconcatService/stringconcat");
        proxy = ChannelFactory<IStringconcat>.CreateChannel(new BasicHttpBinding(), ep);
        proxy.stringconcat(string a, string b);
    }
}

[Guid("E130F97E-1844-4C8B-9E57-AF42632A2557")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IStringconcat_IAMCOM
{
    [DispId(1)]
    string stringconcat(string a, string b);
}

Now i can make calls from c++ to WCF-Services, thats great!