VC에서 만든 DLL을 VB에서 사용할 때 애먹고 있으신 분들을 위해..
참고로, 굳이 프로젝트 세팅에서 __stdcall 방식으로 바꿀 필요는 없다.
이렇게 하면 c 코드를 작성하는 모든 부분의 데이터를 이상하게 처리해야될지 모르므로 -ㅅ-;;
각 함수마다 __stdcall을 이용하면되는데..

예를 들면

int __stdcall a() {}


와 같이 함수 선언부에 __stdcall를 붙이면 이 함수만 __stdcall 규약을 따르게 될 수 있다.

P.S. 보통 Windows API들이 __stdcall을 따른다. 그 이유는, Windows에서 제공하는 API들은 VC, VB, Delphi 등등의 프로그램에서 사용되어야 하므로 표준 규약인 __stdcall을 지키는것이며, 그래서 VC에서 Windows API들이 선언된 것을 보면 WINAPI 라고 된 것을 볼 수 있다. 이 WINAPI는 PASCAL 형이며, PASCAL은 _pascal 의 별칭이다. (wtypes.h에 define 되어 있으며, pascal과 stdcall방식은 서로 같다. pascal방식은 caller가 stack을 삭제하는 것이다.) 이렇게 함으로써, 우리는 쥐도새도모르게(?) Windows에서 제공하는 API를 __stdcall이라는 키워드 없이 이용할 수 있는것이다.

아래는 돌아다니다가 주워온 자료이다.





http://www.vb-helper.com/howto_vcc_dll.html

Title Use VC++ to create a DLL file and use its functions in Visual Basic 6
Description

               
This example shows how to use VC++ to create a DLL file and use its functions in Visual Basic 6.
Keywords VC++, VCC, DLL, C++
Categories Software Engineering


I have found 2 methods to do this. The first makes the VC++ easier and the Visual Basic messier. The second makes the VC++ harder and the Visual Basic easier.

Method 1: Easy VC++, Hard VB

VC++: Invoke New\Projects\Win32 Dynamic-Link Library. Enter the directory where you want the VC++ project and the project name (MyFuncsProject in the example).

VC++: Invoke New\Files\C++ Source File. Check the Add To Project box. Enter the file name (MyFuncs.cpp in the example).

VC++: Enter the function's source code. Use __declspec (note the two underscores) to export the function's symbol. Use 'extern "C"' to minimize name mangling by VC++.

// Define DllExport to declare exported symbols.
#define DllExport __declspec( dllexport )

// Prototype the function.
// Use 'extern "C"' to minimize name mangling.
extern "C" DllExport long MyCFunc(long x);

// Define the function.
extern "C" DllExport long MyCFunc(long x)
{
	return x * x;
}


VC++: Set project options using Project\Settings. On the C/C++ tab, select the Code Generation category. Then change Calling Convention to __stdcall.

VC++: Select Build\Set Active Configuration. Select the Release configuration. Repeat step 4 to make the options apply to the release configuration in addition to the debug configuration. Use Build\Set Active Configuration to reselect the debug configuration if desired.

VC++: Build the project (press F7 or use the Build menu). This creates the DLL file.

VB: In your Visual Basic program, declare the DLL function using the DLL file's full path name. The function's name in the DLL file has been slightly mangled by VC++. The name is an underscore, followed by the name you gave it, followed by "@", followed by the number of bytes in the function's argument list. In this example the name is _MyCFunc@4 because the function takes one 4 byte argument (a long integer).

Private Declare Function MyCFunc Lib "C:\VBHelper\VcDll\Method1\Release\MyFuncsProject.dll" Alias "_MyCFunc@4" (ByVal x As Long) As Long

Private Sub Command1_Click()
	Dim x As Long
	Dim y As Long

	x = CInt(Text1.Text)
	y = MyCFunc(x)
	Label1.Caption = Str$(y)
End Sub


*** HINT: To quickly determine the mangled name of the function, find the DLL file in Windows Explorer. Right click on the file and select the "Quick View" command. This presents an editor showing information about the DLL. Page down 2 or 3 pages and you will find a list of exported symbols available in the DLL. One of these will be the mangled function name.


VB: Run the program.


--------------------------------------------------------------------------------


Method 2, Hard VC++, Easy VB
Steps 1 through 5 are the same as in Method 1.


VC++: New\Text File. Check the Add To Project box. Enter the file name. Give it a .DEF extension (MyFuncs.def in the example).

VC++: Enter definition file information that tells VC++ to export the function with the mangled name using the name you want it to have. The following code makes the exported name MyCFunc equivalent to _MyCFunc@4.

EXPORTS
MyCFunc=_MyCFunc@4


VC++: Build the project (press F7 or use the Build menu). This creates the DLL file.
*** HINT: Use Quick View to verify the exported names. Find the DLL file in Windows Explorer. Right click on the file and select the "Quick View" command. Page down 2 or 3 pages and you will find a list of exported symbols available in the DLL. This includes both the mangled name and the name you specified in the .DEF file.


VB: In your Visual Basic program, declare the DLL function using the DLL file's full path name. Use the name you placed in the .DEF file not the mangled name.

Private Declare Function MyCFunc Lib "C:\VBHelper\VcDll\Method2\Release\MyFuncsProject.dll" (ByVal x As Long) As Long

Private Sub Command1_Click()
	Dim x As Long
	Dim y As Long

	x = CInt(Text1.Text)
	y = MyCFunc(x)
	Label1.Caption = Str$(y)
End Sub

크리에이티브 커먼즈 라이센스
Creative Commons License

Posted by 장현준

2007/06/30 11:34 2007/06/30 11:34
, , , , , ,
Response
No Trackback , a comment
RSS :
http://b4you.net/blog/rss/response/114


블로그 이미지

빗소리를 먹는 사람.

- 장현준

Notices

Archives

Authors

  1. 장현준

Recent Trackbacks

  1. 듀얼클러치의 생각 rsvin28's me2DAY 2009

Calendar

«   2012/02   »
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29      

Site Stats

Total hits:
158007
Today:
77
Yesterday:
228