从DLL文件调用messagebox函数时出现奇怪的字符? [英] Strange characters when call messagebox function from DLL file?

查看:113
本文介绍了从DLL文件调用messagebox函数时出现奇怪的字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用C ++编译的dll文件中调用函数时遇到问题,带有函数代码的dll文件如下:



I see a problem when call function from dll file which compiled in C++, dll file with functions code as follow:

#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;
}





在此,我导出两个函数inputt()和Shooow()。 Dll文件名为TestCCCC.dll。然后我用C#代码调用它们如下:





In this, I export two function inputt() and Shooow(). Dll file named "TestCCCC.dll". Then I call them in C# code as follow:

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();
      }
    }
}







当我运行它时,第一次点击按钮,它会显示一个带有奇怪字符的消息框,而不是aaaaaa!!!?第二次继续点击,它真实地显示aaaaaa,并继续...并真实地展示......



告诉我是什么发生在这个问题?如何编写两个函数inputt()和Shooow()来第一次真正显示?谢谢。



我尝试过:



我试试看考虑这个但不成功!




When I run it, click the button with the first time, it show a message box with strange characters, not "aaaaaa" !!!? Continue to click with the second time, it show truly with "aaaaaa", and continue...and show truly and truly....

Tell me what was happen in this problem? How to code two function inputt() and Shooow() to show truly at the first time? Thanks.

What I have tried:

I try and try to think about this but not success !

推荐答案

您需要将字符串编组为固定的ASCII字符数组,以供C ++代码使用。你的dll也应该保存整个字符串而不仅仅是一个指针,因为不能保证在调用 Shooow()时字符串仍然存在。
You need to marshall the string into a fixed ASCII character array for the C++ code to use. Your dll should also save the entire string rather than just a pointer, as there is no guarantee that the string will still exist at the call to Shooow().


这篇关于从DLL文件调用messagebox函数时出现奇怪的字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆