VB에서 VC DLL을 호출 할때 호출 규약 오류 때문에 DLL을 찾을수 없다고 메세지를 띄울 때 처리하는 방법입니다.
저희 회사는 아직도 VB 6.0을 사용하고 있거던요.(지금도 UI를 VB로 개발해요 ㅠㅠ)
간단히 말하면 VC에서 __stdcall 호출 규약을 사용하여 DLL 함수를 만들구요,
VB쪽에서 함수를 선언할때 DLL 전체 경로를 주고요, 또한 Alias를 설정해 줍니다.
배포할 때 주의 사항은 DLL 전체 경로를 주면 실행시 해당 경로에 DLL을 찾기 때문에 오류가 발생할수 있습니다.
따라서 DLL 전체 경로를 없애야 합니다.(대부분이 DLL을 실행 파일이랑 같이 두니까요)
원본 : http://b4you.net/blog/tag/%ED%98%B8%EC%B6%9C%EA%B7%9C%EC%95%BD
VC에서 만든 DLL을 VB에서 사용할 때 애먹고 있으신 분들을 위해..
참고로, 굳이 프로젝트 세팅에서 __stdcall 방식으로 바꿀 필요는 없다.
이렇게 하면 c 코드를 작성하는 모든 부분의 데이터를 이상하게 처리해야될지 모르므로 -ㅅ-;;
각 함수마다 __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++.
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).
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.
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.
저희 회사는 아직도 VB 6.0을 사용하고 있거던요.(지금도 UI를 VB로 개발해요 ㅠㅠ)
간단히 말하면 VC에서 __stdcall 호출 규약을 사용하여 DLL 함수를 만들구요,
VB쪽에서 함수를 선언할때 DLL 전체 경로를 주고요, 또한 Alias를 설정해 줍니다.
배포할 때 주의 사항은 DLL 전체 경로를 주면 실행시 해당 경로에 DLL을 찾기 때문에 오류가 발생할수 있습니다.
따라서 DLL 전체 경로를 없애야 합니다.(대부분이 DLL을 실행 파일이랑 같이 두니까요)
원본 : http://b4you.net/blog/tag/%ED%98%B8%EC%B6%9C%EA%B7%9C%EC%95%BD
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 LongDim y As
Long x = CInt(Text1.Text)
y = MyCFunc(x)
Label1.Caption = Str$(y)
End Sub
댓글
댓글 쓰기