NI Home > Community > NI Discussion Forums

Measurement Studio for VC++

Reply
Member
Abhijit D. Babar
Posts: 2
0 Kudos

How to create dll in Visual Studio 2008 in Visual C++

Hello,
I have insatlled Visual Studio 2008. I have to create DLL in Project
type Visual C++.
Which Template i have to select to reate dll, MFC DLL or Class
Library?.

I have to call or import this dll in Windows Forms Application. How
can i do this. Please give details procedure to do this.
Active Participant
JonathanN
Posts: 1,491

Re: How to create dll in Visual Studio 2008 in Visual C++

Hi Abhijit,

Since your question appears to be non-NI related, its probably best if you post on the MSDN forums in the future for these types of questions. If you are new to programming in C# or VB.NET, then you probably want to pick up a good .NET book that includes topics on .NET interop (i.e. managed code working with unmanaged APIs). However, with all that said, the basic list of steps you are going to follow is essentially
  1. Create some unmanaged C++ DLL in what type of C++ you want (MFC, Win32, etc)
  2. Choose your .NET language (C#, VB.NET, C++/CLI, etc)
  3. Use .NET's Platform Invoke (PInvoke) to make calls into this unmanaged DLL.

A couple of links that I have saved as my favorites that might help you out include:
Best Regards,
Jonathan N.
National Instruments
Member
NestorG
Posts: 135
0 Kudos

Re: How to create dll in Visual Studio 2008 in Visual C++

Hi Abhijit,

They will all work with your .NET application, but it depends what you want to do with them and how to interface with them. The Class Library is a .NET, managed DLL. The MFC DLL is an unmanaged DLL interfaced with the Microsoft Foundation Class Library - the predecessor to .NET in C++. Both are different technologies used to encapsulate the windows API. Lastly, a Win32 DLL is the most basic of DLLs - providing no encapsulation and typically used now for simple programming (such as no GUI or object oriented programming). Again, it all depends what you want to do with the DLL. For example, if you would like to create dialog boxes from within the DLL (not your application), then you would use the MFC or Class Library DLLs. However, since you will be using the DLL in a .NET application, I would stick to Class Library to make life a lot easier. Be forewarned that developing in managed C++ could be a daunting task.

If you however just want to do simple operations, such as processing data, then go with Win32 DLLs. They can easily be imported to a .NET language (VB/C#) by using the DllImport() function.

Hope this helps Abhijit!

For examples, simply do a search for creating DLLs and you will get tons of information.
Here are just a few:

DLL Tutorial Part I: MFC DLL Basics
http://www.dotnetheaven.com/Uploadfile/mahesh/DLLBasicsPart105192005020909AM/DLLBasicsPart1.aspx?Art...

CodeProject: Win32 vs MFC
http://www.codeproject.com/KB/cpp/mfc_architecture.aspx

Step by Step: Calling C++ DLLs from VC++ and VB - Part 1
http://www.codeproject.com/KB/DLL/XDllPt1.aspx

Creation of a Simple DLL
http://www.codeproject.com/KB/DLL/dll.aspx

Nestor
Member
Clifford
Posts: 1
0 Kudos

Re: How to create dll in Visual Studio 2008 in Visual C++

Dear Sir:

 I succeeded in making DLL functions and implemented on my projects by using Visual C++ 5.0 or 6.0.  This was a very easy job.  I challenged the creation of DLL functions by using Visual Studio 2008.  I tried to create in the same way as the creation of DLL's by VC++ 6.0, but I failed.  I need step-by-step instructions of how to create DLL's by Visual Studio 2008.  Please help me.

 I appreciate your kind help.

 

Member
Pasban
Posts: 1
0 Kudos

Re: How to create dll in Visual Studio 2008 in Visual C++

If we look at how  AFX_CLASS_EXPORT and AFX_CLASS_IMPORT  are defined in afxv_dll.h we see the following.

#define AFX_CLASS_EXPORT __declspec(dllexport)
#define AFX_CLASS_IMPORT __declspec(dllimport)

So, when exporting our classes from our DLL we want the class declarations from the DLL to look like this:-

class __declspec(dllexport) CMyClass : public CObject
{
    ...
}

And, when importing our C++ classes into our application we want the class declarations from the DLL to look like this:-

class __declspec(dllimport) CMyClass : public CObject
{
    ...
}

OK, so here's how I do things.

In the stdafx.h file for the export DLL, include two #defines at the bottom of the file like this:-

#define _MYLIB_DLLAPI_
#define _MYLIB_NOAUTOLIB_

Now, in the main header file for your DLL, say mylib.h (the main 'point of entry' header for your DLL that you will include in you application later), add the following at the top:-

// The following will ensure that we are exporting our C++ classes when
// building the DLL and importing the classes when build an application
// using this DLL.

#ifdef _MYLIB_DLLAPI_
    #define MYLIB_DLLAPI  __declspec( dllexport )
#else
    #define MYLIB_DLLAPI  __declspec( dllimport )
#endif

// The following will ensure that when building an application (or another
// DLL) using this DLL, the appropriate .LIB file will automatically be used
// when linking.

#ifndef _MYLIB_NOAUTOLIB_
#ifdef _DEBUG
#pragma comment(lib, "mylibd.lib")
#else
#pragma comment(lib, "mylib.lib")
#endif
#endif

Now, just declare all the C++ classes you want exported from the DLL like this:-
(Note: Any C++ classes not declared with MYLIB_DLLAPI will not be exported from the DLL)

class MYLIB_DLLAPI CMyClass : public CObject
{
    ...
}

regards,

 

Matt John

---

complete variety of quilts is availabale here!

Member
7KY5TR58711SWED
Posts: 1
0 Kudos

Re: How to create dll in Visual Studio 2008 in Visual C++

Hi There,I am agree with your post.I found this is an informative and interesting post so i think so it is very useful and knowledgeable. I would like to thank you for the efforts you have made in writing this Pass4sure 199-01 article. I am hoping the same best work from you in the future as well. In fact your creative writing ability has inspired me. Really the article is spreading its wings rapidly...

Member
ChinJohn
Posts: 16
0 Kudos

Re: How to create dll in Visual Studio 2008 in Visual C++

Just build a completely example in Visual 2010, hope it will be help.

The DLL is created in c++, then call it from C#.

 

Member
jlyell
Posts: 1
0 Kudos

Re: How to create dll in Visual Studio 2008 in Visual C++

Thanks this helps alot. I've never been that good with creating the dll, so this really helped.! :smileywink:

Member
angaleo9
Posts: 1
0 Kudos

Re: How to create dll in Visual Studio 2008 in Visual C++

Thanks a lot. This is exactly what I was looking for.