"""(e-mail address removed) ÐÉÓÁÌ(Á):
"""
Hi,
I need to develop DLL that exposes API. It should be accessed by C#.
Can any one please help me with that? any tutorial or link would be
greate.
TIA,
Create dll in Visual C++ 2005:
1. "New Project\Win32\Win32 Console Application" create a new project.
-> OK
2. "Application Settings\ Empty project" -> FINISH
3. In "Solution Explorer" on "Source files" add new item "main.cpp"
4. Copy the text:
#include <windows.h>
#include <cstring>
int __declspec(dllexport) FuncA(int i)
{
return i*10;
};
int __declspec(dllexport) FuncB(int i)
{
return i*100;
};
char userLogin[80], userPassword[80];
bool __declspec(dllexport) camomileLogin(char user_name[], char
user_password[])
{
strcpy(userLogin,user_name);
strcpy(userPassword,user_password);
return true;
};
__declspec(dllexport) char* camomileGetUserLogin()
{
return userLogin;
};
5. In "Property Pages\Configuration Properties\General\Configuration
Type" select "Dynamic Library (.dll)" APPLY
6. Build.
You may use the tool ""C:\Program Files\Microsoft Visual Studio
8\Common7\Tools\Bin\Depends.Exe""
Code for C# :
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 camomile_root
{
public partial class Form1 : Form
{
[DllImport("libcamomile.dll", EntryPoint = "?FuncA@@YAHH@Z")]
public static extern int FuncA(int x);
[DllImport("libcamomile.dll", EntryPoint = "?FuncB@@YAHH@Z")]
public static extern int FuncB(int x);
[DllImport("libcamomile.dll", EntryPoint =
"?camomileGetUserLogin@@YAPADXZ")]
public static extern string camomileGetUserLogin();
[DllImport("libcamomile.dll", EntryPoint =
"?camomileLogin@@YA_NQAD0@Z", CharSet = CharSet.Ansi, CallingConvention
= CallingConvention.StdCall)]
public static extern bool camomileLogin(string user_name,
string user_password);
public Form1()
{
InitializeComponent();
}
private void okey_Click(object sender, EventArgs e)
{
int answer = FuncA(57);
userLogin.Text = answer.ToString();
int answer1 = FuncB(57);
userPassword.Text = answer1.ToString();
string s = "user";
string s2 = "passwd";
camomileLogin(s,s2);
string answer2 = camomileGetUserLogin();
userPassword.Text = answer2;
}
private void cancelButton_Click(object sender, EventArgs e)
{
Close();
}
}
}