views:

91

answers:

1

I have an ActiveX control written in C# and working when run in an ActiveX compatible program (CoDeSys). The problem I've come across is that in order to allow CoDeSys to interact with the ActiveX control, CoDeSys requires the dll to export the function prototype:

void ExecuteActiveXCall(IUnknown* pUnk, char* pszId, char* pszParam, char* pszReturnBuffer, int nReturnBufferSize, DWORD* pdwReturnFlag);

I've looked without success on how to export this like you can in C++, as shown in this example:

extern "C" __declspec (dllexport) void ExecuteActiveXCall(IUnknown* pUnk, char* pszId, char* pszParam, char* pszReturnBuffer, int nReturnBufferSize, DWORD* pdwReturnFlag)
{
  if (strcmp(pszId, "IWebBrowser|GoBack") == 0)  
  {
    IUnknown* pNewUnk;
    IWebBrowser* pwb;
    pUnk->QueryInterface(IID_IWebBrowser, (void**) &pNewUnk);
    pwb = (IWebBrowser*) pNewUnk;

    if (pwb)
    {
      pwb->GoBack();
      pwb->Release();
    }
  }
  else if (strcmp(pszId, "IWebBrowser|GoForward") == 0)
  {
    IUnknown* pNewUnk;
    IWebBrowser* pwb;
    pUnk->QueryInterface(IID_IWebBrowser, (void**) &pNewUnk);
    pwb = (IWebBrowser*) pNewUnk;

    if (pwb)
    {
      pwb->GoForward();
      pwb->Release();
    }
  }
}

C# does have an extern keyword, but it doesn't allow you to provide the function definition (at least I haven't found a way). After attempting this:

extern unsafe void ExecuteActiveXCall(
        [MarshalAs(UnmanagedType.IUnknown)] object pUnk, 
        char* pszId, 
        char* pszParam,
        char* pszReturnBuffer, 
        int nReturnBufferSize,
        UInt32* pdwReturnFlag)
    {

    }

The following error occurs:

'AlarmsCSharp.AlarmsControl.ExecuteActiveXCall(object, char*, char*, char*, int, uint*)' cannot be extern and declare a body

Has anyone attempted exporting a function in a C# dll?

Are there any workarounds? (I had the thought of [DllImport("AlarmsCSharp.dll")] and calling C# in the C++ dll, but figured I'd see if anyone had a solution before)

Perhaps I'm over thinking this and don't need to export this function since the ActiveX control is able to interact already with the C# code.

EDIT: I have a feeling my translation from the C++ function prototype to the C# interface declaration. If someone with more experience with C++/C# programming could verify that I did that translation correct or incorrect, it may be of some help.

A: 

You say that CoDeSys is ActiveX-compatible; have you tried using COM interop?

Matthew Graybosch