Domanda

I am new to COM. I have the task to extend an existing COM Server with new functionality. Extending or rewriting existing methods works fine, however, I added a new method that returns a number, but I get an exception (0xC0000005: Access violation writing location...) when writing a simple value to the pointer location, even tough the code is exactly the same as the existing method. The new method is executed, as breakpoints in it are working. What am I doing wrong? It seems as if the server stub does not allocate memory for new methods. The client to test the server method is written in C#. Server and Test client are executed on the same machine.

.idl file:

[id(19)] HRESULT GetLoggingLevel
(
    [out, retval] long* Level
);
[id(190)] HRESULT GetLoggingLevelNew
(
    [out, retval] long* Level
);

.cpp file of COM Server:

STDMETHODIMP DeviceControl::GetLoggingLevel(LONG * Level)
{
    *Level = 43; // works
    return S_OK;
}
STDMETHODIMP DeviceControl::GetLoggingLevelNew(LONG * Level)
{
    *Level = 42; // acces violation writing location [Level]
    return S_OK;
}

Form1.cs of Test client

private void Form1_Load(object sender, EventArgs e)
{
    m_deviceControl = new MechLib.DeviceControl();
    int foo = m_deviceControl.GetLoggingLevel(); // foo = 43
    int bar = m_deviceControl.GetLoggingLevelNew(); // server crashes
    ...
È stato utile?

Soluzione

If you ever change a COM interface you MUST assign a new GUID to it. Failure to do so can result in the wrong proxy/stub being used for your interface and this will likely no nothing of your new method ...

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top