locked
C++.net function callback from unmanaged dll RRS feed

  • Question

  • Hello,

    I am trying to interface dll written probably with Visual C (MFC). Documentation says that one of the functions to call is:

    SI_STATUS SI_GetNumDevices (LPDWORD NumDevices)

    SI_STATUS is strucutre of three members (succes, device not found and invalid parameter). Now NumDevices is an ADDRESS of Int32 (DWORD) variable. Variable on that address will be updated by this dll function. I have to pass address to it. As much as I found out the only way to do it is through delegate.

    Code Block

    //So I declare a delegate, the same type as my dll function:
    public delegate int SI_GetNumDevicesDelegate(Int32^ NumDevices);

    //Declare a prototype of dll function:
    [DllImport("SiUSBXp.dll")]
    extern "C" int SI_GetNumDevices(SI_GetNumDevicesDelegate^ NumDevices);

    //Write a function that will do something as soon as the variable update will come
    private: int SI_GetNumDevicesProc(Int32^ NumDevices) {
    label1->Text = Convert::ToString(*NumDevices);
    // do something
    return 3;
    }

    //Call that function (under Button_click)
    SI_GetNumDevicesDelegate^ myCallBack = gcnew SI_GetNumDevicesDelegate(&UsingDLL::Form1::SI_GetNumDevicesProc);
    SI_GetNumDevices(myCallBack);

    During compile i get error:
    error C3352: 'int UsingDLL::Form1::SI_GetNumDevicesProc(System::Int32 ^)' : the specified function does not match the delegate type 'int (System::Int32 ^)'


    Help please. I can't uderstand the problem.
    | write it on Visual Studio 2005 C++ express edition, CLR, Windows Form App.


    Saulius
    Thursday, November 8, 2007 7:00 PM

Answers

  • Sorry.  I didn't read closely enough.  I read calling C++ from C#.  You want to call a native C++ dll from managed C++.  Try this;

    public:
     [System::Runtime::InteropServices:Big SmilellImport("SiUSBXp.dll", EntryPoint="SI_GetNumDevices", ExactSpelling=false, CharSet=System::Runtime::InteropServices::CharSet::Auto, SetLastError=true)]
     static int SI_GetNumDevices(int %NumDevices);

    Friday, November 9, 2007 2:05 PM

All replies

  • Pass and Int32 by ref:  ref Int32 NumDevices.

    Thursday, November 8, 2007 7:07 PM
  • I did try that, but it is C++ not C#. I get an error C2065: 'ref' : undeclared identifier if put a line like:
    Code Block

    extern "C" int SI_GetNumDevices(ref Int32 NumDevices);


    Friday, November 9, 2007 6:45 AM
  • Sorry.  I didn't read closely enough.  I read calling C++ from C#.  You want to call a native C++ dll from managed C++.  Try this;

    public:
     [System::Runtime::InteropServices:Big SmilellImport("SiUSBXp.dll", EntryPoint="SI_GetNumDevices", ExactSpelling=false, CharSet=System::Runtime::InteropServices::CharSet::Auto, SetLastError=true)]
     static int SI_GetNumDevices(int %NumDevices);

    Friday, November 9, 2007 2:05 PM