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

Visual Basic 6.0 에서의 Regular Expression

1. 메뉴의 프로젝트(P) - 참조(N) 에서 Microsoft VBScript Regular Expressions 5.5 를 체크한다

2. 다음과 같은 코드를 입력한다.

Function TestRegExp(myPattern As String, myString As String)

'Create objects.
Dim objRegExp As RegExp
Dim objMatch As Match
Dim colMatches As MatchCollection
Dim RetStr As String

' Create a regular expression object.
Set objRegExp = New RegExp

'Set the pattern by using the Pattern property.
objRegExp.Pattern = myPattern

' Set Case Insensitivity.
objRegExp.IgnoreCase = True

'Set global applicability.
objRegExp.Global = True

'Test whether the String can be compared.
If (objRegExp.Test(myString) = True) Then

    'Get the matches.
    Set colMatches = objRegExp.Execute(myString)

    For Each objMatch In colMatches ' Iterate Matches collection.

        RetStr = RetStr & objMatch.FirstIndex & ". Match Value is '"
        RetStr = RetStr & objMatch.Value & "'." & vbCrLf

    Next

End If

TestRegExp = RetStr

End Function


3. 이용한다
Debug.Print TestRegExp("haha*", "ha1hahasss")



참고로 정규식은 다음과 같다
Character Description

\

Marks the next character as a special character, a literal, a backreference, or an octal escape. For example, 'n' matches the character "n". '\n' matches a newline character. The sequence '\\' matches "\" and "\(" matches "(".

^

Matches the position at the beginning of the input string. If the RegExp object's Multiline property is set, ^ also matches the position following '\n' or '\r'.

$

Matches the position at the end of the input string. If the RegExp object's Multiline property is set, $ also matches the position preceding '\n' or '\r'.

*

Matches the preceding character or subexpression zero or more times. For example, zo* matches "z" and "zoo". * is equivalent to {0,}.

+

Matches the preceding character or subexpression one or more times. For example, 'zo+' matches "zo" and "zoo", but not "z". + is equivalent to {1,}.

?

Matches the preceding character or subexpression zero or one time. For example, "do(es)?" matches the "do" in "do" or "does". ? is equivalent to {0,1}

{n}

n is a nonnegative integer. Matches exactly n times. For example, 'o{2}' does not match the 'o' in "Bob," but matches the two o's in "food".

{n,}

n is a nonnegative integer. Matches at least n times. For example, 'o{2,}' does not match the "o" in "Bob" and matches all the o's in "foooood". 'o{1,}' is equivalent to 'o+'. 'o{0,}' is equivalent to 'o*'.

{n,m}

m and n are nonnegative integers, where n <= m. Matches at least n and at most m times. For example, "o{1,3}" matches the first three o's in "fooooood". 'o{0,1}' is equivalent to 'o?'. Note that you cannot put a space between the comma and the numbers.

?

When this character immediately follows any of the other quantifiers (*, +, ?, {n}, {n,}, {n,m}), the matching pattern is non-greedy. A non-greedy pattern matches as little of the searched string as possible, whereas the default greedy pattern matches as much of the searched string as possible. For example, in the string "oooo", 'o+?' matches a single "o", while 'o+' matches all 'o's.

.

Matches any single character except "\n". To match any character including the '\n', use a pattern such as '[\s\S].

(pattern)

Matches pattern and captures the match. The captured match can be retrieved from the resulting Matches collection, using the SubMatches collection in VBScript or the $0$9 properties in JScript. To match parentheses characters ( ), use '\(' or '\)'.

(?:pattern)

Matches pattern but does not capture the match, that is, it is a non-capturing match that is not stored for possible later use. This is useful for combining parts of a pattern with the "or" character (|). For example, 'industr(?:y|ies) is a more economical expression than 'industry|industries'.

(?=pattern)

Positive lookahead matches the search string at any point where a string matching pattern begins. This is a non-capturing match, that is, the match is not captured for possible later use. For example 'Windows (?=95|98|NT|2000)' matches "Windows" in "Windows 2000" but not "Windows" in "Windows 3.1". Lookaheads do not consume characters, that is, after a match occurs, the search for the next match begins immediately following the last match, not after the characters that comprised the lookahead.

(?!pattern)

Negative lookahead matches the search string at any point where a string not matching pattern begins. This is a non-capturing match, that is, the match is not captured for possible later use. For example 'Windows (?!95|98|NT|2000)' matches "Windows" in "Windows 3.1" but does not match "Windows" in "Windows 2000". Lookaheads do not consume characters, that is, after a match occurs, the search for the next match begins immediately following the last match, not after the characters that comprised the lookahead.

x|y

Matches either x or y. For example, 'z|food' matches "z" or "food". '(z|f)ood' matches "zood" or "food".

[xyz]

A character set. Matches any one of the enclosed characters. For example, '[abc]' matches the 'a' in "plain".

[^xyz]

A negative character set. Matches any character not enclosed. For example, '[^abc]' matches the 'p' in "plain".

[a-z]

A range of characters. Matches any character in the specified range. For example, '[a-z]' matches any lowercase alphabetic character in the range 'a' through 'z'.

[^a-z]

A negative range characters. Matches any character not in the specified range. For example, '[^a-z]' matches any character not in the range 'a' through 'z'.

\b

Matches a word boundary, that is, the position between a word and a space. For example, 'er\b' matches the 'er' in "never" but not the 'er' in "verb".

\B

Matches a nonword boundary. 'er\B' matches the 'er' in "verb" but not the 'er' in "never".

\cx

Matches the control character indicated by x. For example, \cM matches a Control-M or carriage return character. The value of x must be in the range of A-Z or a-z. If not, c is assumed to be a literal 'c' character.

\d

Matches a digit character. Equivalent to [0-9].

\D

Matches a nondigit character. Equivalent to [^0-9].

\f

Matches a form-feed character. Equivalent to \x0c and \cL.

\n

Matches a newline character. Equivalent to \x0a and \cJ.

\r

Matches a carriage return character. Equivalent to \x0d and \cM.

\s

Matches any white space character including space, tab, form-feed, and so on. Equivalent to [ \f\n\r\t\v].

\S

Matches any non-white space character. Equivalent to [^ \f\n\r\t\v].

\t

Matches a tab character. Equivalent to \x09 and \cI.

\v

Matches a vertical tab character. Equivalent to \x0b and \cK.

\w

Matches any word character including underscore. Equivalent to '[A-Za-z0-9_]'.

\W

Matches any nonword character. Equivalent to '[^A-Za-z0-9_]'.

\xn

Matches n, where n is a hexadecimal escape value. Hexadecimal escape values must be exactly two digits long. For example, '\x41' matches "A". '\x041' is equivalent to '\x04' & "1". Allows ASCII codes to be used in regular expressions.

\num

Matches num, where num is a positive integer. A reference back to captured matches. For example, '(.)\1' matches two consecutive identical characters.

\n

Identifies either an octal escape value or a backreference. If \n is preceded by at least n captured subexpressions, n is a backreference. Otherwise, n is an octal escape value if n is an octal digit (0-7).

\nm

Identifies either an octal escape value or a backreference. If \nm is preceded by at least nm captured subexpressions, nm is a backreference. If \nm is preceded by at least n captures, n is a backreference followed by literal m. If neither of the preceding conditions exist, \nm matches octal escape value nm when n and m are octal digits (0-7).

\nml

Matches octal escape value nml when n is an octal digit (0-3) and m and l are octal digits (0-7).

\un

Matches n, where n is a Unicode character expressed as four hexadecimal digits. For example, \u00A9 matches the copyright symbol (?).

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

Posted by 장현준

2007/01/28 01:41 2007/01/28 01:41
, ,
Response
No Trackback , No Comment
RSS :
http://b4you.net/blog/rss/response/59


블로그 이미지

빗소리를 먹는 사람.

- 장현준

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:
158026
Today:
96
Yesterday:
228