简体   繁体   English

从DLL文件调用消息框功能时出现奇怪的字符?

[英]Strange characters when call messagebox function from DLL file?

I see a problem when call function from dll file which compiled in C++, dll file with functions code as follow: 我从以C ++编译的dll文件调用函数时看到一个问题,该dll文件的函数代码如下:

#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <string.h>

char* str;
int _stdcall inputt(char* str_)
{
    str=str_;
    return 1;
}

int _stdcall Shooow()
{
    MessageBoxA(NULL,str,"Message...",MB_OK);
    return 0;
}

In this, I export two function inputt() and Shooow(). 在此,我导出两个函数inputt()和Shooow()。 Dll file named "TestCCCC.dll". DLL文件名为“ TestCCCC.dll”。 Then I call them in C# code as follow: 然后,我用C#代码对其进行如下调用:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.InteropServices;

namespace muWrapper
{
    public partial class WndSample : Form
    {
        [DllImport("TestCCCC.dll")]
        private static extern int inputt(string FuncName);
        [DllImport("TestCCCC.dll")]
        private static extern int Shooow();

      public WndSample()
      {
        InitializeComponent();
      }

      private void button1_Click(object sender, EventArgs e)
      {
          int ret = inputt("aaaaaa");
          ret = Shooow();
      }
    }
}

When I run it, click the button with the first time, it show a message box with strange characters, not "aaaaaa" !!!? 当我运行它时,第一次单击该按钮,它将显示一个带有奇怪字符的消息框,而不是“ aaaaaa”! Continue to click with the second time, it show truly with "aaaaaa", and continue...and show truly and truly.... 继续单击第二次,它显示为“ aaaaaa”,并继续...并显示为真实。

Tell me what was happen in this problem? 告诉我这个问题发生了什么? How to code two function inputt() and Shooow() to show truly at the first time? 如何编写两个函数inputt()和Shooow()第一次真正显示? Thanks. 谢谢。

inputt is passed a pointer to a temporary string. inputt传递了一个指向临时字符串的指针。 You can't just save the pointer, you'll need to save a copy of the whole string. 您不仅可以保存指针,还需要保存整个字符串的副本。 Easiest to do that with std::string std::string最容易做到这一点

#include "stdafx.h"
#include <windows.h>
#include <string>

static std::string str;
int _stdcall inputt(const char* str_)
{
    str=str_;
    return 1;
}

int _stdcall Shooow()
{
    MessageBoxA(NULL,str.c_str(),"Message...",MB_OK);
    return 0;
}

C# natively supports Unicode strings. C#本机支持Unicode字符串。 If you want to handle arbitrary strings, you will need to change the DLLImport line to: 如果要处理任意字符串,则需要将DLLImport行更改为:

    [DllImport("TestCCCC.dll", CharSet = Unicode)]
    private static extern int inputt(string FuncName);

and then change the C++ to: 然后将C ++更改为:

#include "stdafx.h"
#include <windows.h>
#include <string>

static std::wstring str;  // wide string
int _stdcall inputt(const wchar_t* str_) // wide char pointer.
{
    str=str_;
    return 1;
}

int _stdcall Shooow()
{
    MessageBoxW(NULL,str.c_str(),L"Message...",MB_OK);  // Use W version and long title.
    return 0;
}

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

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