简体   繁体   English

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

[英]Passing std::string to C++ DLL for use with C# dll

I am trying to pass strings from a C++ console app to a C++ dll, which i then convert to System::string to be sent to a C# dll. 我正在尝试将字符串从C ++控制台应用程序传递到C ++ dll,然后我将其转换为System :: string以发送到C#dll。 But the string comes through as random characters. 但是字符串以随机字符的形式出现。 Note that lShutdown actually comes through correct as it is defined in the dll so the problem must be between the testapp and the C++ dll. 请注意,lShutdown实际上是通过dll中定义的正确设置,因此问题必须出在testapp和C ++ dll之间。 I had this working before, but i cant figure out for the life of me how i did it 我以前曾做过这项工作,但我无法弄清楚我是怎么做的

Here is the code for passing to the 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);
}

Here is the dll code: 这是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);
    }

Here is what i actually get through: 这是我实际上得到的:

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>

The characters that come through are different each time the program is run 每次运行程序时遇到的字符都不同

Any help would be greatly appreciated 任何帮助将不胜感激

If you are using C++11 (which you most probably be using), you can try using std::u32string . 如果您使用的是C ++ 11(很可能正在使用),则可以尝试使用std::u32string It removes the need to use std::wstring and is standard across platforms. 它消除了使用std::wstring的需要,并且是跨平台的标准配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM