110,533
社区成员
发帖
与我相关
我的任务
分享
extern "C"
{
__declspec(dllexport) char *my_strcat(char *dst, char *src)
{
char *old_pos = dst;
while (*dst)
{
dst++;
}
while (*dst++ = *src++)
{
;
}
return old_pos;
}
}
//导入刚刚生成的 DLL ,函数入口是 my_strcat ,调用方式是 cdecl ,字符集是 unicode
[DllImport("*.dll", EntryPoint = "my_strcat", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
public static extern string my_strcat(string a, string b);
//在点击事件中把文本框1中的字符串,文本框2中的字符串连接起来,然后把连接的字符串放在文本框3中。
textBox3.Text = my_strcat(textBox1.Text, textBox2.Text);
[DllImport("Cal_Dll.dll", EntryPoint = "my_strcat", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern string my_strcat(string a, string b);
textBox3.Text = my_strcat(textBox1.Text, textBox2.Text);
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//导入刚刚生成的 DLL ,函数入口是 my_strcat ,调用方式是 cdecl ,字符集是 unicode
[DllImport("test.dll", EntryPoint = "my_strcat", CharSet = CharSet.Ansi)]
public static extern string my_strcat(string a, string b);
private void button1_Click(object sender, EventArgs e)
{
//在点击事件中把文本框1中的字符串,文本框2中的字符串连接起来,然后把连接的字符串放在文本框3中。
textBox3.Text = my_strcat(textBox1.Text, textBox2.Text);
}
}
}
extern "C"
{
__declspec(dllexport) char *my_strcat(char *dst, char *src)
{
char *old_pos = dst;
while (*dst)
{
dst++;
}
while (*dst++ = *src++)
{
;
}
return old_pos;
}
}
vs2008调用成功。
// 危险代码!!
int i = 0;
char src[] = "hello";
my_strcat(src, "buffer overflow");
printf("i=%x", i); //i=776f6c66
比如以上代码,src缓冲区的实际大小只有6(五个字符加上一个零休止)。
把另外一个字符串接在它后面,就可能修改了不是src缓冲区的其他数据。
比如以上试验中,i就被覆盖了(不同编译器可能有不同内存布局,可能有不同结果)。如果覆盖的不只是int i,而是影响了线程堆栈,或者其他重要信息,程序就可能崩溃了,甚至可能把你C盘格式化。
你在C#中用string来传入dst,这种情况下dst一点都没有预留空间,几乎肯定要缓冲溢出。
因此,解决问题的关键就要要保证dst主够大,比如:
StringBuilder dst = new StringBuilder(1024); //预留1024个字符
dst.Append("hello");
my_strcat(des, " world");
但是,问题是要预留多大的空间?1024会不会浪费?1024会不会太小?
更好的解决方法,就是明确指示缓冲区的大小。比如,换成如下导出:
void my_str(char* dst, const char* src, int dst_length)
{
int offset = strnlen(dst, dst_length);
for(int i = offset; i < dst_length && *src; i++)
{
dst[i] = *src++;
}
}
其中src用const char*修饰,const表示不会被改变,因此可以安全的用C# string传入。
其中dst则可以被改变,它的大小用dst_length来限制。
这也是为什么strcat被标记被不安全代码,而strncat被要求使用的原因。
[DllImport("*.dll", EntryPoint = "my_strcat", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
public static extern IntPtr my_strcat(string a, string b);
IntPtr pStr = my_strcat(textBox1.Text, textBox2.Text);
textBox3.Text = Marshal.PtrToStringAnsi(pStr);
结果不是乱码了,但是只是把两个字符串的首字母连接起来了,盼好心人能指点一二,给出代码以供参考,并能指出我错在哪里。谢谢了。
参考资料如下:
http://blog.csdn.net/xinyaping/article/details/7288164
在此特别感谢金辉软件技术公司的唐先生对我的帮助,谢谢所有帮助过我的人。