● C言語で DLL を作って VB から呼び出す ●

あまり詳しくはないのでお手柔らかに。個人的な覚え書き。とりあえず構造体の取得、文字列の取得あたりを押さえておけばよいかと。int型は WORD と表記した方がよいのかな?

General.h ファイル
// DllMain関数の宣言
BOOL WINAPI DllMain(HANDLE hInst, ULONG ul_reason_for_call, LPVOID lpReserved);

General.cpp ファイル
#include <windows.h>
#include "General.h"

// 初期化関数の本体
BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved){
    return TRUE; // 常に正常終了
}

DllSample.def ファイル
LIBRARY DllSample

EXPORTS
    GetGreet          @1
    GetZipCode       @2

DllSample.h ファイル
#include <windows.h>
#include <stdio.h>

#define GREET_LENGTH  20

typedef struct GREET {
    char Morning[GREET_LENGTH];   //朝
    char Afternoon[GREET_LENGTH]; //昼
    char Night[GREET_LENGTH];     //夜
    int  CountryID;               //国番号
} GREET;

//関数プロトタイプ
extern "C" __declspec( dllexport )
    void __stdcall GetGreet(int CID, struct Greet *udtGreet);
extern "C" __declspec( dllexport )
    int __stdcall GetZipCode(const LPSTR InZip, LPSTR OutZip, int OutZipLen);

DllSample.cpp ファイル
#include "DllSample.h"

//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//  関数名: GetGreet
//  機  能: 挨拶を返す
//  引  数: (i)CID      … 適当なID
//           (o)udtGreet … Greet構造体
//  返り値: なし
//  備  考: 特になし
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
__declspec( dllexport ) 
    void __stdcall GetGreet(int CID, struct GREET *udtGreet) {

    //初期化
    memset(udtGreet, '\0', sizeof(GREET));

    switch(CID){
        case 0:
            strcpy(udtGreet->Morning,   "Good Morning");
            strcpy(udtGreet->Afternoon, "Good Afternoon");
            strcpy(udtGreet->Night,     "Good Night");
            udtGreet->CountryID = 1;
            break;
        default:
            strcpy(udtGreet->Morning,   "おはよう");
            strcpy(udtGreet->Afternoon, "こんにちは");
            strcpy(udtGreet->Night,     "こんばんは");
            udtGreet->CountryID = 81;
            break;
    }

}

//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//  関数名: GetZipCode
//  機  能: 郵便番号編集を行う
//  引  数: (i)InZip  … 郵便番号
//           (o)OutZip … 編集後郵便番号
//           (i)OutZipLen … OutZipバッファサイズ
//  返り値: なし
//  備  考: 特になし
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
__declspec( dllexport ) 
int __stdcall GetZipCode(const LPSTR InZip, LPSTR OutZip, int OutZipLen) {
    memset(OutZip, '\0', OutZipLen);
    strncpy(OutZip, InZip, 3);
    strcat(OutZip, "-");
    strcat(OutZip, InZip + 3);
    return strlen((LPSTR) OutZip);
}

上記をガッツリとコンパイルして、DllSample.dll を作成する。


以下は Visual Basic での使用方法!!

Private Const GREET_LENGTH As Long = 20

Private Type GREET
    Morning As String * GREET_LENGTH    '朝
    Afternoon As String * GREET_LENGTH  '昼
    Night  As String * GREET_LENGTH     '夜
    CountryID As Long                   'ID
End Type

Private Declare Sub GetGreet Lib "DllSample" Alias "#1" (ByVal CID As Long, ByRef udtGreet As GREET)
Private Declare Function GetZipCode Lib "DllSample" Alias "#2" (ByVal InZip As String, ByVal OutZip As String, ByVal OutZipLen As Long) As Long

'---------------------------------------------------------------------------
' 関数名 : GetGreetProc
' 機能   : 国別の挨拶を取得する
' 引数   : なし
' 返り値 : なし
'---------------------------------------------------------------------------
Public Sub GetGreetProc()
    Dim udtGreet As GREET
    Call GetGreet(1, udtGreet)
    With udtGreet
        Debug.Print Left$(.Morning, InStr(.Morning, Chr$(0)) - 1)
        Debug.Print Left$(.Afternoon, InStr(.Afternoon, Chr$(0)) - 1)
        Debug.Print Left$(.Night, InStr(.Night, Chr$(0)) - 1)
        Debug.Print .CountryID
    End With
End Sub

'---------------------------------------------------------------------------
' 関数名 : GetZipCodeProc
' 機能   : 郵便番号編集を行う
' 引数   : なし
' 返り値 : なし
'---------------------------------------------------------------------------
Public Sub GetZipCodeProc()
    Dim OutBuff As String * 10
    Debug.Print GetZipCode("1234567", OutBuff, Len(OutBuff))
    Debug.Print Left$(OutBuff, InStr(OutBuff, Chr$(0)) - 1)
End Sub

で、実行!!

Private Sub Command1_Click()
    Call GetGreetProc
    Call GetZipCodeProc
End Sub

[出力結果]
おはよう
こんにちは
こんばんは
 81 
 8 
123-4567

戻る