使用C++从Windows应用程序向dll方法传递字符串

Passing string to dll mehods from Windows application using C++

本文关键字:方法 字符串 dll C++ Windows 应用程序 使用      更新时间:2023-10-16

我们目前正在创建一个简单的Windows应用程序,该应用程序将加载dll文件并在dll中调用不同的方法。

以下是状态:

  1. Windows应用程序设计-->确定
  2. 从UI单击加载.dll按钮-->确定
  3. 从UI单击按钮时调用.dll文件中的方法-->确定
  4. 单击-->NOK按钮,将字符串传递给.dll方法

.dll中的方法

    sendReqXml(char *xmlString) 
    {   
    printf("nsendReqXml() +n");
    //we will write xmlstring in file here to check what we received  from  UI console
    FILE *out;
    if ((out = fopen("C:\Users\20105388\Desktop\textfile.txt", "w")) != NULL)
     {
       fprintf(out, "the range is '%s'n", xmlString);
       fclose(out);
     }
      //.... then proceed further 
    }

UI控制台方法

private: System::Void button2_Click(System::-Object^  sender, System::EventArgs^  e)
{
  char *xmlFile = "I am sending text from UI";
  DLLSEND _SENDXML;
  _SENDXML = (DLLSEND) GetProcAddress(hGetProcIDDLL, "sendReqXml");
 if (_SENDXML != NULL)
   {
     _SENDXML(xmlFile);
   }
}

我们只是试图在sendReqXml方法的文件中打印一个字符串,以检查我们从UI接收到的内容。

  fprint(out,"the range is '%s'n", xmlString); 

无论我们从UI传递什么字符串,上面的方法都会在文件中写入下面的输出

  **the range is '„¹&Z'**

这是不正确的,那么有人能帮助解决这个问题吗?

注意:

当使用命令行时,上面的方法可以很好地工作,并在文件中写入正确的输出。

谢谢你的帮助。

我认为您正在尝试调用托管c++(c++/cli)上的本机c/c++dll函数。正确的那么…你试过PInvoke吗?参考本页https://msdn.microsoft.com/en-us/library/ms235282.aspx