How to fix a C # wrapper for C ++?

200 Views Asked by At

I am trying to call a function from a dll. Description of the function in C ++:

BOOL WINAPI PDLCSGetPropertyEx(LPCTSTR lpszProjectName, LPCTSTR lpszPictureName, LPCTSTR lpszObjectName, LPCTSTR lpszPropName, VARTYPE vt, LPVOID pvProp, DWORD dwFlags, LPVOID pData, PCMN_ERROR pError);

In C # I have written:

[DllImport("pdlcsapi.dll", EntryPoint = "PDLCSGetPropertyEx", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
    public static extern bool PDLCSGetPropertyEx(
        [In] String lpszProjectName,
        [In] String lpszPictureName,
        [In] String lpszObjectName,
        [In] String lpszPropName,
        VarEnum vt,
         IntPtr pvProp,
        [In] UInt32 dwFlags,
        [In] IntPtr pData,
        [In,Out] [MarshalAs(UnmanagedType.LPStruct)] CMN_ERROR_MANCLASS pError
        );

and the call in the application:

WinCCODK_PDLCS.CMN_ERROR_MANCLASS errPdl;
        errPdl = new WinCCODK_PDLCS.CMN_ERROR_MANCLASS();
        IntPtr p = new IntPtr();
        String propName = listBox2.SelectedItem.ToString();
        String objName = listBox1.SelectedItem.ToString();
        bool ret = WinCCODK_PDLCS.CPDLCSWrapper.PDLCSGetPropertyEx(
            "C:\\DemoProjectV72_Light\\DemoProjectV72_Light.mcp"
            , "io.pdl"
            , objName
            , propName
            , VarEnum.VT_I4
            , p
            , 0
            , IntPtr.Zero
            , errPdl ); 
        if (ret){
            listBox2.Items[listBox2.SelectedIndex] = listBox2.Items[listBox2.SelectedIndex] + val.ToString();
        }else{
            MessageBox.Show(errPdl.szErrorText);
        }

After calling this function, the application falls (there is a standard Windows window stopped working program)

How to pass parameters to a function?

Other features of this library are working fine. .

I think that does not work correctly with the parameters vt and PvProp. These are described in the documentation:

vt
Data type of the value passed with pvProp. Valid types are defined in the "VARENUM" enumeration within the "wtypes.h" Include file belonging to the compiler. No VT_VARIANT, VT_DISPATCH or other references should be used.

PvProp
Pointer to a tag to which the property value is saved. The data type of the value is determined by vt. For pvProp you can specify any value defined for the Variant data type; refer to the values contained in the "wtypes.h" Include file belonging to your compiler. For types which have a buffer (e.g. BSTR), the buffer is allocated by the function and must be cleared afterwards by the calling application.

0

There are 0 best solutions below