"""Er********@g mail.com ÐÉÓÁÌ(Á):
"""
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\W in32 Console Application" create a new project.
-OK
2. "Applicatio n 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(dlle xport) FuncA(int i)
{
return i*10;
};
int __declspec(dlle xport) FuncB(int i)
{
return i*100;
};
char userLogin[80], userPassword[80];
bool __declspec(dlle xport) camomileLogin(c har user_name[], char
user_password[])
{
strcpy(userLogi n,user_name);
strcpy(userPass word,user_passw ord);
return true;
};
__declspec(dlle xport) char* camomileGetUser Login()
{
return userLogin;
};
5. In "Property Pages\Configura tion Properties\Gene ral\Configurati on
Type" select "Dynamic Library (.dll)" APPLY
6. Build.
You may use the tool ""C:\Progra m Files\Microsoft Visual Studio
8\Common7\Tools \Bin\Depends.Ex e""
Code for C# :
using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
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("libc amomile.dll", EntryPoint = "?FuncA@@YAHH@Z ")]
public static extern int FuncA(int x);
[DllImport("libc amomile.dll", EntryPoint = "?FuncB@@YAHH@Z ")]
public static extern int FuncB(int x);
[DllImport("libc amomile.dll", EntryPoint =
"?camomileGetUs erLogin@@YAPADX Z")]
public static extern string camomileGetUser Login();
[DllImport("libc amomile.dll", EntryPoint =
"?camomileLogin @@YA_NQAD0@Z", CharSet = CharSet.Ansi, CallingConventi on
= CallingConventi on.StdCall)]
public static extern bool camomileLogin(s tring user_name,
string user_password);
public Form1()
{
InitializeCompo nent();
}
private void okey_Click(obje ct sender, EventArgs e)
{
int answer = FuncA(57);
userLogin.Text = answer.ToString ();
int answer1 = FuncB(57);
userPassword.Te xt = answer1.ToStrin g();
string s = "user";
string s2 = "passwd";
camomileLogin(s ,s2);
string answer2 = camomileGetUser Login();
userPassword.Te xt = answer2;
}
private void cancelButton_Cl ick(object sender, EventArgs e)
{
Close();
}
}
}