DLL を作成して利用する

  目次







  C++ ビルド用バッチファイル



  
Set CL6="C:\Program Files\Microsoft Visual Studio\VC98\Bin\cl.exe"
Set LINK6="C:\Program Files\Microsoft Visual Studio\VC98\Bin\link.exe"
Set INC6="C:\Program Files\Microsoft Visual Studio\VC98\Include"
Set INCA="C:\Program Files\Microsoft Visual Studio\VC98\ATL\Include"
Set LIB6="C:\Program Files\Microsoft Visual Studio\VC98\Lib"
Set LIBS1=kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib
Set LIBS2=shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib
%CL6% mydll.cpp /c /MD /I%INC6% /I%INCA%
%LINK6% /DLL /LIBPATH:%LIB6% /OUT:mydll.dll /DEF:mydll.def mydll.obj %LIBS1% %LIBS2%
  



  mydll.def と mydll.cpp

  
LIBRARY	"mydll.dll"

EXPORTS
	MsgBox	@1
  

  
#include <windows.h>
#include "stdio.h"

extern "C" {  
__declspec(dllexport) void __stdcall MsgBox( LPTSTR lpMessage )
{

	HWND hWnd;

	hWnd = GetDesktopWindow();

	MessageBox( hWnd, lpMessage, "MsgBox", MB_OK );


}}
  

内部情報
  
Dump of file mydll.dll

File Type: DLL

  Section contains the following exports for mydll.dll

           0 characteristics
    467D390B time date stamp Sun Jun 24 00:15:23 2007
        0.00 version
           1 ordinal base
           1 number of functions
           1 number of names

    ordinal hint RVA      name

          1    0 00001000 MsgBox

  Summary

        1000 .data
        1000 .rdata
        1000 .reloc
        1000 .text
  



  VB からの呼び出し

  
vbc.exe load_01.bas
  

  
Module App

' ********************************************************
' * DLL 内 関数宣言
' ********************************************************
Declare Function MyMsgBox Lib "mydll.dll" Alias _
"MsgBox" (ByVal lpMessage As String) As Integer

Class MyNamespace
	Declare Function MsgBox Lib "mydll.dll" Alias _
	"MsgBox" (ByVal lpMessage As String) As Integer
End Class

' ********************************************************
' * 実行
' ********************************************************
Sub Main()

	MsgBox( "日本語表示" )
	MyMsgBox( "日本語表示" )
	MyNamespace.MsgBox( "日本語表示" )

End Sub

End Module
  


  
vbc.exe load_02.bas
  

クラス名に "Class" という文字列を含ませるとエラーになります
  
Class App
' ********************************************************
' * DLL 内 関数宣言
' ********************************************************
Declare Function MyMsgBox Lib "mydll.dll" Alias _
"MsgBox" (ByVal lpMessage As String) As Integer

Class MyNamespace
	Declare Function MsgBox Lib "mydll.dll" Alias _
	"MsgBox" (ByVal lpMessage As String) As Integer
End Class

' ********************************************************
' * 実行
' ********************************************************
Shared Sub Main()

	MsgBox( "日本語表示" )
	MyMsgBox( "日本語表示" )
	MyNamespace.MsgBox( "日本語表示" )

End Sub

End Class
  


  
vbc.exe load_03.bas
  

クラス名に "Class" という文字列を含ませるとエラーになります
  
' ********************************************************
' * DLL 内 関数宣言
' ********************************************************
Class MyNamespace
	Declare Function MsgBox Lib "mydll.dll" Alias _
	"MsgBox" (ByVal lpMessage As String) As Integer
End Class

Class App
' ********************************************************
' * 実行
' ********************************************************
Shared Sub Main()

	MsgBox( "日本語表示" )
	MyNamespace.MsgBox( "日本語表示" )

End Sub

End Class
  



  RunDll32.exe から呼び出せる dll

  
#include <windows.h>
#include "stdio.h"

extern "C" {  
__declspec(dllexport) void __stdcall MsgBox
(HWND hWnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow)
{
	char buff[1024];

	wsprintf( buff, "%s : %d", lpszCmdLine, nCmdShow );

	MessageBox( hWnd, buff, "MsgBox", MB_OK );
}}
  

  
RunDll32.exe mydll.dll,MsgBox 日本語表示
  
↑実行



  VB で コンポーネント( dll ) を作成する

lightbox.dll が作成されます

  
vbc /target:library lightbox.bas
  

  
Option Explicit On
Option Strict On

' **********************************************************
' StringBuilder 利用
' **********************************************************
Imports System.Text

' **********************************************************
' Imports 対象名
' **********************************************************
Namespace Lightbox

' **********************************************************
' クラス名
' **********************************************************
Public Class LboxString

	Private Buffer As new StringBuilder( 1024 )

	' ****************************************
	' コンストラクタ1
	' ****************************************
	Public Sub New( str As String )
		MyBase.New
		Buffer.Append( str )
	End Sub
	' ****************************************
	' コンストラクタ2
	' ****************************************
	Public Sub New( size As Integer )
		MyBase.New
		Buffer.Length = size
	End Sub

	' ****************************************
	' 部分文字列取得
	' ****************************************
	Public Function GetString(ByVal size As Integer) _
		As String
		GetString = Buffer.ToString( 0, size )
	End Function

	' ****************************************
	' 文字列サイズ取得
	' ****************************************
	ReadOnly Property Length() As Integer
		Get
			Length = Buffer.ToString().Length
		End Get
	End Property

End Class

End Namespace
  



  VB でユーザーコンポーネントの呼び出し

  
vbc /reference:lightbox.dll use.bas
  

  
Imports Lightbox

Module App

' ********************************************************
' * 実行
' ********************************************************
Sub Main()

	Dim str As new LboxString( "日本語表示" )

	Console.WriteLine( str.GetString(1) )
	Console.WriteLine( str.GetString(2) )
	Console.WriteLine( str.Length )

	Dim str2 As new LboxString( 10 )

	Console.WriteLine( "|" & str2.GetString(1) & "|" )
	Console.WriteLine( "|" & str2.GetString(2) & "|" )
	Console.WriteLine( str2.Length )

End Sub

End Module
  

  
日
日本
5
| |
|  |
10
  










  infoboard   管理者用   
このエントリーをはてなブックマークに追加





フリーフォントWEBサービス
SQLの窓WEBサービス

SQLの窓フリーソフト

素材

一般WEBツールリンク

SQLの窓

フリーソフト

JSライブラリ