文書番号: 402464
最終更新日: 1997/10/23
--------------------------------------------------------------------------- Declare Sub name Lib "library name" (argument list) Declare Function name Lib "library name" (argument list) As return type ---------------------------------------------------------------------------
--------------------------------------------------------------------------- Declare Sub name Lib "library name" Alias "Bad name" (argument list) Declare Function name Lib "library name" Alias "Bad name" (argument _ list) As return type ---------------------------------------------------------------------------
整数へのポインタ (LPINT) variable name As Integer 長整数へのポインタ (LPDWORD) variable name As Long 整数 (INT、WORD、BOOL etc.)、ハンドル (hWnd、hDC etc.) ByVal variable name As Integer 長整数 (DWORD、LONG) ByVal variable name As Long void へのポインタ (void *) variable name As Any
ByVal variable name As String文字列は常に「参照渡し」になります。ByVal キーワードは「値渡し」という意味では なく、ASCIIZ 文字列 (C 言語などで使用される 0 で終了する文字列) に変換して
Declare Sub name Lib "library name" (variable name As Any) ... name(array name(0))また、Huge 配列 (64 KB よりも大きな配列) の場合は最初の 64 KB だけにしかアクセス することができませんので注意が必要です。
Declare Sub name Lib "library name" (variable name As Any) ... name(ByVal 0&)
----------------------------------------------------------------------------- Option Explicit Declare Function noparams Lib "PASSV.DLL" () As Integer Declare Function passint Lib "PASSV.DLL" (ByVal a As Integer) As Integer Declare Function passlong Lib "PASSV.DLL" (ByVal a As Long) As Long Declare Function passfloat Lib "PASSV.DLL" (ByVal a As Single) As Single Declare Function passdouble Lib "PASSV.DLL" (ByVal a As Double) As Double Declare Sub passpnt Lib "PASSV.DLL" (a As Integer, b As Long, c As Single, _ d As Double) Declare Sub passstr Lib "PASSV.DLL" (ByVal a As String) Declare Sub passary Lib "PASSV.DLL" (a As Integer, ByVal b As Integer) Declare Sub passstrct Lib "PASSV.DLL" (a As teststruct) 'Declare Sub passstrct Lib "passv.dll" (a As Any) ' As Any でも可 Type teststruct id As Integer ld As Long fd As Single dd As Double sd As String * 6 End Type 'ダイアログ表示プログラム Sub showDlg() Dim icnt As Integer For icnt = 0 To 4 DialogSheets("Dialog1").EditBoxes("edi" & icnt).Text = "" Next DialogSheets("Dialog1").Show End Sub 'ボタン btn0 ~ btn7 までがクリックされた時の処理 Sub cmdCallFunc_Click(Index As Integer) Dim idt As Integer Dim ldt As Long Dim sdt As Single Dim ddt As Double Dim icnt As Integer Dim cst As String ReDim iary(1 To 4) As Integer Dim tstruct As teststruct Dim dlg As DialogSheet Set dlg = DialogSheets("Dialog1") For idt = 1 To 4 dlg.EditBoxes("edi" & idt).Text = "" Next idt If Index <> 5 And Val(dlg.EditBoxes("edi0").Text) = 0 Then MsgBox ("数値を入力してください") 'txtValue(0).SetFocus dlg.Focus = dlg.EditBoxes("edi0").Name Exit Sub End If Select Case Index Case 0: ' int 型の値を引数で渡す、結果は戻り値 dlg.EditBoxes("edi" & "1").Text = _ passint(CInt(dlg.EditBoxes("edi0").Text)) Case 1: ' long 型の値を引数で渡す、結果は戻り値 dlg.EditBoxes("edi" & "2").Text = _ passlong(CLng(dlg.EditBoxes("edi0").Text)) Case 2: ' single 型の値を引数で渡す、結果は戻り値 dlg.EditBoxes("edi" & "3").Text = _ passfloat(CSng(dlg.EditBoxes("edi0").Text)) Case 3: ' double 型の値を引数で渡す、結果は戻り値 dlg.EditBoxes("edi" & "4").Text = _ passdouble(CDbl(dlg.EditBoxes("edi0").Text)) Case 4: ' int/long/single/double 型のアドレスを引数で渡す結果は引数自身 idt = CInt(dlg.EditBoxes("edi0").Text) ldt = CLng(dlg.EditBoxes("edi0").Text) sdt = CSng(dlg.EditBoxes("edi0").Text) ddt = CDbl(dlg.EditBoxes("edi0").Text) passpnt idt, ldt, sdt, ddt dlg.EditBoxes("edi1").Text = idt dlg.EditBoxes("edi2").Text = ldt dlg.EditBoxes("edi3").Text = sdt dlg.EditBoxes("edi4").Text = ddt Case 5: ' string 型の文字列記述子を引数で渡す、結果は引数自身 cst = dlg.EditBoxes("edi0").Text If Len(cst) < 2 Then cst = cst + " " passstr cst dlg.EditBoxes("edi1").Text = cst Case 6: ' int 型の配列および要素数を引数で渡す、結果は引数自身 passary iary(1), 4 For icnt = 1 To 4 dlg.EditBoxes("edi" & icnt).Text = iary(icnt) Next icnt Case 7: ' 構造体を引数で渡す、結果は引数自身 tstruct.id = CInt(dlg.EditBoxes("edi0").Text) tstruct.ld = CLng(dlg.EditBoxes("edi0").Text) tstruct.fd = CSng(dlg.EditBoxes("edi0").Text) tstruct.dd = CDbl(dlg.EditBoxes("edi0").Text) tstruct.sd = dlg.EditBoxes("edi0").Text passstrct tstruct dlg.EditBoxes("edi1").Text = tstruct.id dlg.EditBoxes("edi2").Text = tstruct.ld dlg.EditBoxes("edi3").Text = tstruct.fd dlg.EditBoxes("edi4").Text = tstruct.dd dlg.EditBoxes("edi0").Text = tstruct.sd End Select End Sub 'ボタン btn0 ~ btn7 にそれぞれ登録するプログラム Sub btn0_Click() cmdCallFunc_Click (0) End Sub Sub btn1_Click() cmdCallFunc_Click (1) End Sub Sub btn2_Click() cmdCallFunc_Click (2) End Sub Sub btn3_Click() cmdCallFunc_Click (3) End Sub Sub btn4_Click() cmdCallFunc_Click (4) End Sub Sub btn5_Click() cmdCallFunc_Click (5) End Sub Sub btn6_Click() cmdCallFunc_Click (6) End Sub Sub btn7_Click() cmdCallFunc_Click (7) End Sub 'ダイアログが表示される時の初期設定 (フォーカスの設定) 'フォーム 1 に登録するプログラム Sub フォーム 1_Show() DialogSheets("Dialog1").Focus = _ DialogSheets("Dialog1").EditBoxes("edi0").Name End Sub -----------------------------------------------------------------------------
----------------------------------------------------------------------------- #define STRICT #include <windows.h> #include <string.h> /* 関数型: int, 引数: int, 戻り値: 引数の 2 倍 */ int far _pascal _export passint(int a) { return a * 2; } /* 関数型: long, 引数: long, 戻り値: 引数の 2 倍 */ long far _pascal _export passlong(long a) { return a * 2; } /* 関数型: float, 引数: float, 戻り値: 引数の 2 倍 */ float far _pascal _export passfloat(float a) { return a * (float)2.0; } /* 関数型: double, 引数: double, 戻り値: 引数の 2 倍 */ double far _pascal _export passdouble(double a) { return a* (double)2.0; } /* 関数型: void, 引数: int, long, float, double のポインタ */ /* 結果: 引数の値を 2 倍にして返す */ void far _pascal _export passpnt(int _far *a, long _far *b, float _far *c, double _far *d) { *a = *a * 2; *b = *b * 2; *c = *c * (float)2.0; *d = *d * (double) 2.0; return; } /* 関数型: void, 引数: int の配列 */ void far _pascal _export passary(int _far a[], int idx) { int i; for (i=0;i<idx;++i){ a[i] = i; } return; } /* 関数型: void, 引数: 構造体, 結果: 各要素の値を 2 倍にして返す */ typedef struct TESTSTRUCT{ int id; long ld; float fd; double dd; char sd[6]; }FAR * LPSTRUCT; void far _pascal _export passstrct(LPSTRUCT lps) { lps->id = lps->id * 2; lps->ld = lps->ld * 2; lps->fd = lps->fd * (float)2; lps->dd = lps->dd * (double)2; _fstrncpy(lps->sd,"change",6); return; } /* 関数型: void, 引数: char のポインタ, 結果: 文字列の先頭 2 バイトを */ /* ** にして返す */ /* void far _pascal _export passstr(LPSTR a) */ void far _pascal _export passstr(char _far *a) { _fstrncpy (a, "**",2); return; } -----------------------------------------------------------------------------
----------------------------------------------------------------------------- LIBRARY PASSV EXETYPE WINDOWS DATA PRELOAD MOVABLE SINGLE CODE PRELOAD MOVABLE DISCARDABLE -----------------------------------------------------------------------------
----------------------------------------------------------------------------- Declare Function LoadLibrary Lib "Kernel" (ByVal f$) As Integer Declare Sub FreeLibrary Lib "Kernel" (ByVal h As Integer) Dim hInst As Integer hInst = LoadLibrary(library name) If hInst > 32 Then MsgBox "LoadLibrary success" FreeLibrary (hInst) Else MsgBox "LoadLibrary error: " & Format$(hInst) End If -----------------------------------------------------------------------------
Keywords: KBHOWTO KB402464
Technology: kbExcel500 kbExcelSearch kbExcelWinSearch