1:vc6中建立win32 簡單動態連結程式庫
2:添加Rename.cpp檔案,如下:
#include <string>
#include <stdlib.h>
using namespace std;
extern "C" int _declspec(dllexport) renamefile(char *_oldname,char * _newname);
int renamefile(char* _oldname,char* _newname)
{
char oldname[1000], newname[1000];
strcpy(oldname,_oldname);
strcpy(newname,_newname);
if (rename(oldname, newname) == 0)
return 0;
else
return 1;
}
3:產生Rename.dll
4:在C#中調用
複製Rename.dll到應用程式BIN目錄下
C#程式:
using System.Runtime.InteropServices;
namespace WinApp
{
public partial class Form4 : Form
{
[DllImport("Rename.dll", EntryPoint = "renamefile")]
public static extern int renamefile(string source, string dest);
public Form4()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Text = renamefile(@"c:\1.txt", @"c:\2.txt").ToString();
}
}
}
附件:vc6 Rename程式/Files/94cool/Rename.rar
Rename.dll/Files/94cool/Rename.dll.rar