none
C# 叫用C開發的dll時, 出現NotSupportedException錯誤 RRS feed

  • 問題

  • Dear 大大,
    我用C# 叫用C開發的dll時, 出現NotSupportedException的錯誤訊息, Sample Code如下:
            [System.Runtime.InteropServices.DllImport("RDINT.dll", EntryPoint = "RDINTsys_OpenReader")]

            private static extern Int32 RDINTsys_OpenReader(byte u8COMPort, Int32 u32Baudrate,[MarshalAs(UnmanagedType.LPStr)] String strAccessCode, byte u8SecurityMode, Int32 u32OpenDelayMs, Int32 pu32Baudrate);

            public Form1()
            {
                InitializeComponent();
            }

            private void Form1_Load(object sender, EventArgs e)
            {

                try
                { RDINTsys_OpenReader(2, 19200, "0x00000000", 0x00, 1000, 0); }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

    C的檔頭
    RDINT.h 如下:

    #ifndef __RDINT__
    #define __RDINT__
    #include <windows.h>

    #ifndef RDINT_API
    #define RDINT_API
    #endif

    #ifdef _UNICODE
     #define RDINTsys_OpenReader    RDINTsys_OpenReaderW
     #define RDINTsys_OpenReaderReadyTime RDINTsys_OpenReaderReadyTimeW
     #define RDINTsys_GetAPIVersionString RDINTsys_GetAPIVersionStringW
     #define RDINTsys_GetErrorString   RDINTsys_GetErrorStringW
     #define RDINTsys_MifareCardSNtoString RDINTsys_MifareCardSNtoStringW
     #define RDINTv2_KbdGetString   RDINTv2_KbdGetStringW
     #define RDINTv2_LoadKeyFile    RDINTv2_LoadKeyFileW
     #define RDINTv2_ReaderInfo    RDINTv2_ReaderInfoW
    #else
     #define RDINTsys_OpenReaderReadyTime RDINTsys_OpenReaderReadyTimeA
     #define RDINTsys_OpenReader    RDINTsys_OpenReaderA
     #define RDINTsys_GetAPIVersionString RDINTsys_GetAPIVersionStringA
     #define RDINTsys_GetErrorString   RDINTsys_GetErrorStringA
     #define RDINTsys_MifareCardSNtoString RDINTsys_MifareCardSNtoStringA
     #define RDINTv2_LoadKeyFile    RDINTv2_LoadKeyFileA
     #define RDINTv2_KbdGetString   RDINTv2_KbdGetStringA
     #define RDINTv2_ReaderInfo    RDINTv2_ReaderInfoA
    #endif

    #ifdef __cplusplus
    extern "C"{
    #endif

    RDINT_API INT32 WINAPI RDINTsys_OpenReaderA(BYTE u8COMPort, UINT32 u32Baudrate, LPSTR strAccessCode, BYTE u8SecurityMode, UINT32 u32OpenDelayMs, PUINT32 pu32Baudrate);


    胡了啦
    2010年1月12日 上午 06:20

解答

  • 以下的做法是成功的:
    RDINT.h

    #ifndef __RDINT__
    #define __RDINT__
    #include <windows.h>

    #ifndef RDINT_API
    #define RDINT_API
    #endif

    #ifdef _UNICODE
     #define RDINTsys_OpenReader    RDINTsys_OpenReaderW
     #define RDINTsys_OpenReaderReadyTime RDINTsys_OpenReaderReadyTimeW
     #define RDINTsys_GetAPIVersionString RDINTsys_GetAPIVersionStringW
     #define RDINTsys_GetErrorString   RDINTsys_GetErrorStringW
     #define RDINTsys_MifareCardSNtoString RDINTsys_MifareCardSNtoStringW
     #define RDINTv2_KbdGetString   RDINTv2_KbdGetStringW
     #define RDINTv2_LoadKeyFile    RDINTv2_LoadKeyFileW
     #define RDINTv2_ReaderInfo    RDINTv2_ReaderInfoW
    #else
     #define RDINTsys_OpenReaderReadyTime RDINTsys_OpenReaderReadyTimeA
     #define RDINTsys_OpenReader    RDINTsys_OpenReaderA
     #define RDINTsys_GetAPIVersionString RDINTsys_GetAPIVersionStringA
     #define RDINTsys_GetErrorString   RDINTsys_GetErrorStringA
     #define RDINTsys_MifareCardSNtoString RDINTsys_MifareCardSNtoStringA
     #define RDINTv2_LoadKeyFile    RDINTv2_LoadKeyFileA
     #define RDINTv2_KbdGetString   RDINTv2_KbdGetStringA
     #define RDINTv2_ReaderInfo    RDINTv2_ReaderInfoA
    #endif

    #ifdef __cplusplus
    extern "C"{
    #endif

    RDINT_API INT32 __declspec(dllexport) WINAPI RDINTsys_OpenReaderA(BYTE u8COMPort, UINT32 u32Baudrate, LPSTR strAccessCode, BYTE u8SecurityMode, UINT32 u32OpenDelayMs, PUINT32 pu32Baudrate);

    #ifdef __cplusplus
    }
    #endif

    #endif //__RDINT__

    呼叫的寫法:

    [System.Runtime.InteropServices.DllImport("RDINT.dll")]
            private static extern Int32 RDINTsys_OpenReaderA(byte u8COMPort, Int32 u32Baudrate, [MarshalAs(UnmanagedType.LPStr)] String strAccessCode, byte u8SecurityMode, Int32 u32OpenDelayMs, Int32 pu32Baudrate);

            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    RDINTsys_OpenReaderA(2, 19200, "0x00000000", 0x00, 1000, 0); }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

    2010年1月12日 上午 08:11