遇到一个问题,开发winform的时候调用动态库代码:
[DllImport("Mwic_32.dll", EntryPoint="ic_init",  SetLastError=true,
 CharSet=CharSet.Auto , ExactSpelling=false,
 CallingConvention=CallingConvention.StdCall)]
//说明:初始化通讯接口
public static extern int ic_init(Int16 port, int baud);
这个是成功的。没有什么问题。
现在要开发一个 win store App应用,同样要调用这个第三方动态库,可是win store App应用程序开发时CharSet枚举没有Auto 值,所以导致调用该函数永远是失败的问题。现在想是否可以这此动态库之上再封装一个动态库来实现对之前动态库函数的重写,这个要怎么实现,求教~~~或者哪位高人有好的办法能解决此问题,谢谢~~~~望高人指点,最好有代码,谢谢!!!!!

解决方案 »

  1.   

    把CharSet=CharSet.Auto  改成 CharSet=CharSet.Ansi  或 CharSet=CharSet.Unicode试下
      

  2.   

    应该是有一个基类或接口,声明dll的函数,后面派生出类,根据你的平台不同,做不同设置的引入dll
      

  3.   

    给个列子: API FindWindow
    ---
    /// Return Type: HWND->HWND__*
        ///lpClassName: LPCSTR->CHAR*
        ///lpWindowName: LPCSTR->CHAR*
        [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint="FindWindowA")]
    public static extern  System.IntPtr FindWindowA([System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)] string lpClassName, [System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)] string lpWindowName) ;  /// Return Type: HWND->HWND__*
        ///lpClassName: LPCWSTR->WCHAR*
        ///lpWindowName: LPCWSTR->WCHAR*
        [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint="FindWindowW")]
    public static extern  System.IntPtr FindWindowW([System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)] string lpClassName, [System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)] string lpWindowName) ;
      

  4.   

    刚网上查了一下win store App开发调用win32.dll时会有问题,不知道怎么把win32.dll封装成winrt.dll请教~~
      

  5.   

    我感觉不是 charset 的问题。
    先看看你的 函数是否可以在 Windows Runtime 下运行。
    很多函数使用了 Windows Runtime 不支持的函数。 
      

  6.   

    +1 
    charset就是小问题用
     
    这位兄弟的方法就能解决的。
      

  7.   

    metro style app只能使用Win32 API的一个子集,用DllImport调用的动态连接库也要满足这个要求,可以用的API列表如下:
    Win32 and COM for Windows Store apps
    这个和desktop app下的api差异还是很大的,通常老的win32 dll都得要用Metro SDK重新编译并且要能够通过 Windows App Certification,这个不是通过封装就能变成winrt下可用的,明华的dll里用了很多desktop的api(用DependencyWalker就能看到),所以除非他们提供winrt版本,否则h还是放弃吧。