将std :: string传递给C ++ DLL以与C#dll一起使用

时间:2018-01-22 14:46:13

标签: c++ dll

我正在尝试将字符串从C ++控制台应用程序传递到C ++ dll,然后我将其转换为System :: string以发送到C#dll。但字符串是随机字符。请注意,lShutdown实际上是正确的,因为它在dll中定义,所以问题必须在testapp和C ++ dll之间。我之前有过这个工作,但我无法弄清楚我的生活是如何做到的

以下是传递给dll的代码:

void GMSTestAppC::GMSSetup()
{
    string processName = "GMSCTestApp";
    string progName = "TestApp";
    //Directory where program is installed
    string progDir = "C:/";
    string version = "4.0.0.1";
    //Directory for config file
    string configDir = "C:/";
    //Directory for log files
    string logDir = "C:/";
    string user = "Smurf";
    string shutdownby = "";
    Initialize(processName, GetCurrentProcessId(), progName, progDir, version, configDir, logDir, user, shutdownby);
}

这是dll代码:

bool Initialize(std::string& processName, int processID, std::string& programName, std::string& programDir, std::string& version, std::string& configDir, std::string& logDir, std::string& currentUser, std::string& lastShutDownBy)
    {
        String^ pName = gcnew String(processName.c_str());
        String^ progName = gcnew String(programName.c_str());
        String^ progDir = gcnew String(programDir.c_str());
        String^ cDir = gcnew String(configDir.c_str());
        String^ lDir = gcnew String(logDir.c_str());
        String^ cUser = gcnew String(currentUser.c_str());
        String^ lShutdown = gcnew String("test");
        GMS::GMSVersion fv;
        sscanf_s(version.c_str(), "%d.%d.%d.%d", &fv.Major, &fv.Minor, &fv.Revision, &fv.Build);
        int pID = int(processID);

        Return GMS::Initialize(pName, pID, progName, progDir, fv, cDir, lDir, cUser, lShutdown);
    }

这是我实际得到的:

Sent: <InitializeProgram>
      <TimeStamp>2018-01-22T14:34:37.6152709+00:00</TimeStamp>
      <PID>13504</PID>
      <ProcessName>(÷'</ProcessName>
      <ProgramName>Ðð'</ProgramName>
      <ProgramDir>°ñ'</ProgramDir>
      <Version>0.0.0.0</Version>
      <ConfigDir>^ô'</ConfigDir>
      <LogDir>èñ'</LogDir>
      <CurrentUser>÷'</CurrentUser>
      <LastShutDownBy>test</LastShutDownBy>
    </InitializeProgram>

每次运行程序时出现的字符都不同

非常感谢任何帮助

1 个答案:

答案 0 :(得分:0)

如果您使用的是C ++ 11(最常用的),可以尝试使用std::u32string。它消除了使用std::wstring的需要,并且是跨平台的标准。