调用非托管DLL并返回字符串

时间:2017-08-11 11:51:33

标签: c# c++ c dll remedy

我正在尝试调用非托管DLL并使用它返回的内容进行身份验证。有人可以帮我纠正我的代码以返回正确的身份验证字符串吗?

我对struct's感到困惑,任何帮助都会受到赞赏。

这是我调用库的C#代码

     class Wrapper
{
    [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, Pack = 1)]
    public  struct ARSSOUserCredentialsStruct
    {
        /// int
        public int m_nARTCPNum;
        public int m_nARRPCNum;
        /// string*
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)]
        public static string m_szARUserName;
        public string m_szARPassword;
        public string m_szARAuthenticationstring;
        public int m_nARNumberOfServers;
        //bool
        public bool m_bARUsingPreferenceServer;
    }
    public struct ARSSOServerInformation
    {

        /// int

        public int m_nARTCPNum;
        public int m_nARRPCNum;

        /// string*
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)]
        public string m_szARServerName;

    }


    [System.Runtime.InteropServices.DllImportAttribute("ARSSOInfo.dll", EntryPoint = "ARGetSSOLoginCrendentials")]
    public static extern System.IntPtr ARGetSSOLoginCrendentials(string m_szARUserName);

    public static IntPtr getInfo(string m_szARUserName)
    {
       IntPtr ptr = ARGetSSOLoginCrendentials(m_szARUserName); // should this be IntPtr?
        //return (ARSSOUserCredentialsStruct)(Marshal.PtrToStructure(ptr, typeof(ARSSOUserCredentialsStruct)));   // what should type >

       // var ptr = Marshal.AllocHGlobal(Marshal.SizeOf(m_szARUserName));
        if(ptr != null)
        {
            Marshal.StructureToPtr(m_szARUserName, ptr, false);
            Console.WriteLine("Not null");
            Console.WriteLine();

        }
        else
        {
            Console.WriteLine("null");
            Console.ReadLine();
        }
        return ptr;
    }


    class Program
    {
        static void Main(string[] args)
        {
            getInfo("jp");

        }
    }

}

C#app调用的非托管DLL代码:

#include <string.h>
#include <stdafx.h>
struct ARSSOServerInformation
{
    char * m_szARServerName;
    int m_nARTCPNum;
    int m_nARRPCNum;
};
struct ARSSOUserCredentialsStruct
{
    char* m_szARUserName;
    char* m_szARPassword;
    char* m_szARAuthenticationString;
    bool m_bARUsingPreferenceServer;
    int m_nARNumberOfServers;
};

extern "C"
{
    __declspec(dllexport) void
        ARGetSSOLoginCrendentials(ARSSOUserCredentialsStruct
            *pUserCredentialStruct)
    {
        // The required memory for struct ARSSOUserCredentialsStruct is allocated by user tool,
        // This dll just needs to assign values.
        // eg:
        strcpy(pUserCredentialStruct->m_szARUserName, "Demo");
        pUserCredentialStruct->m_nARNumberOfServers = 2;
    }
}//End 'extern "C"

extern "C"
{
    __declspec(dllexport) void
        ARGetSSOServerInformation(ARSSOServerInformation *pServerInfo)
    {
        // The required memory for struct ARSSOServerInformation is allocated by user tool,
        // This dll just needs to assign values.
        // eg:
        strcpy(pServerInfo[0].m_szARServerName, "ServerName1");
        pServerInfo->m_nARTCPNum = 3040; pServerInfo->m_nARRPCNum
            = 390622;
        strcpy(pServerInfo[1].m_szARServerName, "ServerName2");
    }
}//End 'extern "C"

0 个答案:

没有答案