Calling C DLL from C#

后端 未结 4 1415
悲哀的现实
悲哀的现实 2020-12-14 23:47

I am trying to call a C DLL from C#, but I\'m not having any joy. The documentation for the DLL provides an example function delaration for VB that looks like;



        
4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-15 00:17

    You may want to define your c# signatures using marshalling attributes for your string parameters.

    [DllImport(@"c:\GDAit.dll")]
    public static extern long TransGeogPt([MarshalAs(UnmanagedType.LPStr)] string sGridFile, long lDirection, double dLat, double dLong, ref double pdLatNew, ref double pdLongNew, ref double pdLatAcc, ref double pdLongAcc);
    
    [DllImport(@"c:\GDAit.dll")]
    public static extern long TransProjPt([MarshalAs(UnmanagedType.LPStr)] string sGridFile, long lDirection, double dLat, double dLong, long lZone, ref double pdLatNew, ref double pdLongNew, ref double pdLatAcc, ref double pdLongAcc);
    

    I'll also piggy-back on Mark Sowul's answer and say try calling with StdCall instead of Cdecl.

    Also, as a precaution, I'd probably double-check to make sure that the compiler is set to compile x86 code, in case its compiling for 64-bit.

提交回复
热议问题