473,545 Members | 2,714 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

createdll tutorial VC++ 2005

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,

Nov 23 '06 #1
6 1368
Imm

"""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();
}
}
}

Nov 24 '06 #2
Not an ideal solution, because you have to import mangled names, which can
be annoying to figure out, and the mangled names change if you add a funcion
parameter.
A better method is so insure that the function names are not mangled.
The easiest ways to do this are either change
int __declspec(dlle xport) FuncA(int i)
to

extern "c"
{
__declspec(dlle xport) int __cdecl FuncA(int i);
}

int FuncA(int i)
{
//..implementatio n here
}
or add a DEF file to your project and declare function exports in there.
that way you can also export unmangled functions that use the stdcall
calling convention.
[DllImport("libc amomile.dll", EntryPoint = "?FuncA@@YAHH@Z ")]
public static extern int FuncA(int x);

Do not foget to use the CallingConventi on attribute to specify the calling
convention that was used. otherwise there will be an access violation.

--

Kind regards,
Bruno van Dooren
br************* *********@hotma il.com
Remove only "_nos_pam"
Nov 24 '06 #3
Er********@gmai l.com wrote:
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,
Here is a link to a good tutorial on setting up a project to build an
unmanaged DLL:
http://www.codeguru.com/cpp/cpp/cpp_...cle.php/c9855/

I followed this article and created a very simple test DLL (using a
..DEF file for exporting functions).
Here is the C++ file:

#include <iostream>
#include "Test_DLL.h "

#ifdef __cplusplus
extern "C" {
#endif

int Add(int a, int b) {
return( a + b );
}

void Function( void ) {
std::cout << "DLL Called!" << std::endl;
}

#ifdef __cplusplus
}
#endif
Here is the Header file:

#ifndef _DLL_TUTORIAL_H _
#define _DLL_TUTORIAL_H _
#include <iostream>

#ifdef __cplusplus
extern "C" {
#endif

int Add(int a, int b);
void Function( void );

#ifdef __cplusplus
}
#endif

#endif
Here is the DEF file:

LIBRARY Test_DLL
EXPORTS
Add @1
Function @2

Once you have your unmanaged DLL, test it from an unmanaged application
to make sure everything works.
This builds to a .DLL file and a .LIB file. We need these two files
plus the header file for an application to use the DLL.
Here is a simple test program that tests our DLL:
#include <iostream>
#include "Test_DLL.h "

int main()
{
Function();
std::cout << Add(32, 58) << "\n";
return(1);
}

Now you need to build a MANAGED DLL (assembly) so you can call your
functions from C# (or VB or any other .NET language).
Here is the managed C++ file for the managed DLL:

using namespace System::Runtime ::InteropServic es;

[DllImport("Test _DLL", EntryPoint="Add ")]
extern "C" int Add(int a, int b);

[DllImport("Test _DLL", EntryPoint="Fun ction")]
extern "C" void Function( void );

namespace Test_DLL_CLR {

public ref class Test_DLL_Class {
public:
// Function: Add
int Add_clr(int a, int b)
{
return( Add( a, b) );
}

// Function: Function
void Function_clr(vo id)
{
Function();
}
};
}
Once you have your managed DLL, you can include it in your C# project
as a reference. You should now be able to use the namespace defined in
the managed DLL (along with everything defined in that namespace).
Here is C# code that uses the managed DLL:

using System;
using System.Collecti ons.Generic;
using System.Text;
using Test_DLL_CLR;

namespace Test_DLL_CLR_ap p_cs
{
class Program
{
static void Main(string[] args)
{
Test_DLL_Class myclass = new Test_DLL_Class( );
int val;

Console.WriteLi ne("C# Test_DLL_CLR Test Program");

myclass.Functio n_clr();
val = myclass.Add_clr (123, 456);

Console.WriteLi ne("val = " + val);

Console.WriteLi ne("Finished.") ;
}
}
}

Nov 24 '06 #4

<Er********@gma il.comwrote in message
news:11******** **************@ f16g2000cwb.goo glegroups.com.. .
Hi,

I need to develop DLL that exposes API. It should be accessed by C#.
Does it need to be used from any non-.NET language?
Can any one please help me with that? any tutorial or link would be
greate.
building a C++/CLI assembly for consumption by C# is trivially easy, just do
new project -C++ -CLR -Class Library, then add some "ref class"
definitions (there's a wizard for that too, right click the C++ project and
do Add -New Class).
>
TIA,

Nov 24 '06 #5

Imm wrote:
"""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();
}
}
}
Hi All and thanks for your reply,

Imm,I have question regarding your answer.

I changed the functions name in the dll. when i changed the Entry
points in C#, with the same name, I got massage that the entry points
can't be found. I changed function FuncA to GetPosition and FuncB to
SetPosition. I also changed in C#:
[DllImport("Yass o.dll", EntryPoint = "?FuncA@@YAHH@Z ")]
public static extern int FuncA(int x);
to
[DllImport("Yass o.dll", EntryPoint = "?GetPosition@@ YAHH@Z")]
public static extern int GetPosition(int x);

Why is it not working?
TIA,

Nov 24 '06 #6
Imm
Hi All and thanks for your reply,
>
Imm,I have question regarding your answer.

I changed the functions name in the dll. when i changed the Entry
points in C#, with the same name, I got massage that the entry points
can't be found. I changed function FuncA to GetPosition and FuncB to
SetPosition. I also changed in C#:
[DllImport("Yass o.dll", EntryPoint = "?FuncA@@YAHH@Z ")]
public static extern int FuncA(int x);
to
[DllImport("Yass o.dll", EntryPoint = "?GetPosition@@ YAHH@Z")]
public static extern int GetPosition(int x);

Why is it not working?
TIA,
I have not problem. You mayby don't rewrite your new version dll and
you C# see old version.

See my source code. You passible found your misteke. I changed FuncA to
GetPosition and rewrite dll to folder with C# executable file. This is
All.

See other answers. its very good.

#include <windows.h>
#include <cstring>
int __declspec(dlle xport) GetPosition(int i) // it was change
{ 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;
};

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 testdllCSharp
{ public partial class Form1 : Form
{ [DllImport("test dll.dll", EntryPoint = "?GetPosition@@ YAHH@Z")]

public static extern int GetPosition(int x);// <--- it was
change ^
[DllImport("test dll.dll", EntryPoint = "?FuncB@@YAHH@Z ")]
public static extern int FuncB(int x);
[DllImport("test dll.dll", EntryPoint =
"?camomileGetUs erLogin@@YAPADX Z")]
public static extern string camomileGetUser Login();
[DllImport("test dll.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 button1_Click(o bject sender, EventArgs e)
{ int answer = GetPosition(57) ;// it was change
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;
}
}
}

Nov 27 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2295
by: ultranet | last post by:
I have cruised around http://msdn.microsoft.com/visualc/ and the rest of the site, and i am not able to find a single C++ or VC++ certification exam that will be available after June 30, 2004. I emailed support, and the reply was: "We understand your concerns in this matter. We would like to inform you that at this moment we do not have...
1
965
by: Hosalli | last post by:
Can I save my work done in VC++ 2005 Express Edition so as to work in VC++ 6.0? If yes how do I do it? I need this as I've got to submit my assignments to my instructor who uses VC++ 6.0. Thanks
2
2277
by: um | last post by:
When the POSIX pthreads library for w32 release 2-2-0 (http://sources.redhat.com/pthreads-win32/) is compiled with VC++6 then it compiles and passes all the benchmark tests in the subdirectory "tests". Also, VC++ 2005 beta 1 compiles the tests fine, but here the following tests fail in execution: # semaphore1.pass \ # condvar2.pass \...
8
1282
by: david_75 | last post by:
I wonder if the transistion from the project written in VC++ 6.0 to VC++ .NET requires a lot of code changes (if any) if I compile the project in native code (or unmanaged code) without using the CLR in VC++ .NET. Thanks for your feedback.
7
1697
by: Mihajlo Cvetanović | last post by:
Hi all, I've been trying to find some info on Visual C++ 2005 Standard on Microsoft's site, but wasn't able to find any. There's only VC++ 2005 Express Edition, and Visual Studio 2005 Standard, Professional and Team System. What is the future of VC++ 2005 Standard? Can I somehow buy only VC++ in VS 2005 Standard?
1
2483
by: Max Wilson | last post by:
Hi, Has anyone here built Boost.Python modules under MinGW? I'm trying to build the Boost.Python tutorial under MinGW and getting an error that says it depends on MSVC, which puzzles me because Boost built using g++. Here's some of my output: Student@YGGDRASIL /c/Boost/libs/python/example/tutorial $ bjam -sTOOLS=mingw -d+2 ....found...
0
1391
by: sandy123 | last post by:
Hi there, I am looking for a tutorial/user guide for MS VC++ 6 IDE (which is a part of the MS Visual Studio 6). - The tutorial should explain the use of VC++ IDE. - It should explain all the menu options/ functionalities provided as a part of the IDE. - It should also explain about the debugger and other tools that are often used...
6
2630
by: meyzhong | last post by:
Hi, I am new to Visual C++ (6.0). I want to put all the print information in different lines into a box. I create a EDIT dialogue box, and then add a CString variable (m_MSG) to the dialogue box to display the print information in the box. However, the print information displayed in the box is in one line, like this "10.0| 20.0". The...
7
8431
by: Norman Diamond | last post by:
A project depends on VC runtime from Visual Studio 2005 SP1, and DotNet Framework 2. Options are set in the setup project properties, so if these two dependencies are not already installed then this installer will install them. But what about the situation where VC runtime has already been installed? In fact it's been installed twice. ...
0
7502
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7434
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7692
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7946
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7457
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6026
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5360
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
744
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.