1

我正在尝试调用非托管 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# 应用程序调用的非托管 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"
4

0 回答 0