怎么用C#调用C++写的Dll文件
如何用C#调用C++写的Dll文件
请问C++这一边怎么写?C#那边又如何调用?能结合例子就最好了。
------解决方案--------------------
有些问题百度比在这儿问答案来得快些
请问C++这一边怎么写?C#那边又如何调用?能结合例子就最好了。
------解决方案--------------------
有些问题百度比在这儿问答案来得快些
作者:易庆华(转载请注明出处)
/* C语言源代码 (dllmin.c)*/
/* Replace "dll.h" with the name of your header */
#include "dll.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
DLLIMPORT void HelloWorld ()
{
MessageBox (0, "Hello!JiaYi Studio!\n", "Hi", MB_ICONINFORMATION);
}
DLLIMPORT void OutDir_To_Txt()
{
system("dir >>Dir.txt");/*输出当前目录到Dir文件*/
}
BOOL APIENTRY DllMain (HINSTANCE hInst /* Library instance handle. */ ,
DWORD reason /* Reason this function is being called. */ ,
LPVOID reserved /* Not used. */ )
{
switch (reason)
{
case DLL_PROCESS_ATTACH:
break;
case DLL_PROCESS_DETACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
}
/* Returns TRUE on success, FALSE on failure */
return TRUE;
}
/*dll.h
JiaYi Studio(yqh2648)
*/
#ifndef _DLL_H_
#define _DLL_H_
#if BUILDING_DLL
# define DLLIMPORT __declspec (dllexport)
#else /* Not BUILDING_DLL */
# define DLLIMPORT __declspec (dllimport)
#endif /* Not BUILDING_DLL */
DLLIMPORT void HelloWorld (void);
DLLIMPORT void OutDir_To_Txt(void);
#endif /* _DLL_H_ */
//C#调用,请将编译好的DLL文件拷贝到C#工程输出目录
//嘉怡工作室 湖南省岳阳县荣家湾镇城南村三组
//E-Mail:yqh2648@163.com
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 WindowsApplication1
{
public partial class Form1 : Form
{
/*[DllImport("MyAdd.dll", SetLastError = true)]
private static extern int Add(int x, int y);
[DllImport("MyAdd.dll", SetLastError = true)]
private static extern IntPtr HelloWorld();
[DllImport("MyAdd.dll", SetLastError = true)]
private static extern char Mystr(char a);*/
[DllImport("OutDir.dll", SetLastError = true)]//我用C语言输出的DLL文件名为OutDir.dlll
private static extern IntPtr OutDir_To_Txt();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
/* int x = 6;
int y = 9;
int z = Add(x, y);
MessageBox.Show(z.ToString ());*/
}
private void button2_Click(object sender, EventArgs e)
{
/* HelloWorld();*/
}