動的 DLL 利用

  目次



実行時に DLL を参照して実行します





  スタンダード



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

// ********************************************************
// * 外部関数の型宣言
// ********************************************************
typedef UINT (__stdcall *LPFUNC_1)
(
	LPTSTR lpBuffer,
	UINT uSize
);
typedef BOOL (__stdcall *LPFUNC_2)
(
	LPTSTR lpBuffer,
	LPDWORD lpnSize
);
typedef HANDLE (__stdcall *LPFUNC_3)
(
	LPCTSTR lpFileName,
	LPWIN32_FIND_DATA lpFindFileData
);
typedef BOOL (__stdcall *LPFUNC_4)
(
	HANDLE hFindFile,
	LPWIN32_FIND_DATA  lpFindFileData
);


int main(int argc, char* argv[])
{

	char buff[512];
	HINSTANCE lib;

	lib = LoadLibrary( "kernel32.dll" );
	if ( lib == NULL ) {
		printf( "kernel32.dll のロードに失敗しました\n" );
		return 0;
	}

	// *******************************************
	// システムディレクトリ表示
	// *******************************************
	LPFUNC_1 GetSystemDirectory;
	GetSystemDirectory = (LPFUNC_1)GetProcAddress( lib, "GetSystemDirectoryA" );
	if ( GetSystemDirectory == NULL ) {
		FreeLibrary( lib );
		printf( "%s\n", "GetSystemDirectory のアドレスの取得に失敗しました");
		return 0;
	}
	GetSystemDirectory( buff, 512 );
	printf( "%s\n", buff );

	// *******************************************
	// PC名表示
	// *******************************************
	LPFUNC_2 GetComputerName;
	GetComputerName = (LPFUNC_2)GetProcAddress( lib, "GetComputerNameA" );
	if ( GetComputerName == NULL ) {
		FreeLibrary( lib );
		printf( "%s\n", "GetComputerName のアドレスの取得に失敗しました");
		return 0;
	}
	DWORD nSize;
	GetComputerName( buff, &nSize );
	printf( "%s\n", buff );

	// *******************************************
	// ファイル一覧
	// *******************************************
	LPFUNC_3 FindFirstFile;
	FindFirstFile = (LPFUNC_3)GetProcAddress( lib, "FindFirstFileA" );
	if ( FindFirstFile == NULL ) {
		FreeLibrary( lib );
		printf( "%s\n", "FindFirstFile のアドレスの取得に失敗しました");
		return 0;
	}
	LPFUNC_4 FindNextFile;
	FindNextFile = (LPFUNC_4)GetProcAddress( lib, "FindNextFileA" );
	if ( FindNextFile == NULL ) {
		FreeLibrary( lib );
		printf( "%s\n", "FindNextFile のアドレスの取得に失敗しました");
		return 0;
	}


	WIN32_FIND_DATA wfd;
	HANDLE hHandle;
	int ret;

	hHandle = FindFirstFile( "C:\\tmp\\*.*", &wfd );
	ret = -1;

	while( hHandle != (HANDLE)-1 && ret != 0 ) {

		printf( "%-10d %s\n", wfd.nFileSizeLow, wfd.cFileName );
		ret = FindNextFile( hHandle, &wfd );

	}

	FreeLibrary( lib );

	return 0;
}

  



  VB.NET + Framework 2.0

  
Imports System.Text
Imports System.Runtime.InteropServices

Module MyModule

' ********************************************************
' * WIN32_FIND_DATA 構造体と同等のクラス定義
' ********************************************************
< StructLayout( LayoutKind.Sequential, CharSet := CharSet.Ansi )> _
Public Class WIN32_FIND_DATA
	Public fileAttributes As Integer = 0
	Public creationTime_lowDateTime As Integer = 0
	Public creationTime_highDateTime As Integer = 0
	Public lastAccessTime_lowDateTime As Integer = 0
	Public lastAccessTime_highDateTime As Integer = 0
	Public lastWriteTime_lowDateTime As Integer = 0
	Public lastWriteTime_highDateTime As Integer = 0
	Public nFileSizeHigh As Integer = 0
	Public nFileSizeLow As Integer = 0
	Public dwReserved0 As Integer = 0
	Public dwReserved1 As Integer = 0
	< MarshalAs( UnmanagedType.ByValTStr, SizeConst := 256 )> _
	Public fileName As String = Nothing
	< MarshalAs( UnmanagedType.ByValTStr, SizeConst := 14 )> _
	Public alternateFileName As String = Nothing
End Class

' ********************************************************
' * DLL 内 関数宣言
' ********************************************************
Declare Function LoadLibrary Lib "kernel32" Alias _
"LoadLibraryA" (ByVal lpDllName As String) As Integer

Declare Function FreeLibrary Lib "kernel32" Alias _
"FreeLibrary" (ByVal hModule As Integer) As Integer

Declare Function GetProcAddress Lib "kernel32" Alias _
"GetProcAddress"(ByVal hModule As Integer, lpProcName As String) As Integer

' ********************************************************
' * 外部関数の型宣言
' ********************************************************
Delegate Function GetSystemDirectory( _
ByVal lpBuffer As StringBuilder, ByVal nSize As Integer) As Integer

Delegate Function GetComputerName( _
ByVal lpBuffer As StringBuilder, ByRef nSize As Integer) As Integer

Delegate Function FindFirstFile( _
ByVal fileName As String, _
<[In], Out> ByVal findFileData As WIN32_FIND_DATA _
) As Integer

Delegate Function FindNextFile( _
ByVal hFindFile As Integer, _
<[In], Out> ByVal findFileData As WIN32_FIND_DATA _
) As Integer


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

	Dim strPath As new StringBuilder( 512 )

	Dim hModule As Integer = LoadLibrary("kernel32.dll")

	' ********************************************************
	' システムディレクトリ表示
	' ********************************************************
	Dim ptr As IntPtr
	ptr = GetProcAddress(hModule, "GetSystemDirectoryA")
	if ptr <> IntPtr.Zero then
		Dim func1 As GetSystemDirectory = _
			Marshal.GetDelegateForFunctionPointer( _
				ptr, _
				GetType(GetSystemDirectory) _
			)

		Call func1( strPath, strPath.Capacity )
	end if
	Call System.Console.WriteLine( strPath )

	' ********************************************************
	' PC名表示
	' ********************************************************
	ptr = GetProcAddress(hModule, "GetComputerNameA")
	if ptr <> IntPtr.Zero then
		Dim func2 As GetComputerName = _
			Marshal.GetDelegateForFunctionPointer( _
				ptr, _
				GetType(GetComputerName) _
			)

		Dim nSize As Integer = 512
		Call func2( strPath, nSize )
	end if
	Call System.Console.WriteLine( strPath )

	' ********************************************************
	' ファイル一覧
	' ********************************************************
	Dim func3 As FindFirstFile
	Dim func4 As FindNextFile

	ptr = GetProcAddress(hModule, "FindFirstFileA")
	if ptr <> IntPtr.Zero then
		func3 = _
			Marshal.GetDelegateForFunctionPointer( _
				ptr, _
				GetType(FindFirstFile) _
			)

	end if
	ptr = GetProcAddress(hModule, "FindNextFileA")
	if ptr <> IntPtr.Zero then
		func4 = _
			Marshal.GetDelegateForFunctionPointer( _
				ptr, _
				GetType(FindNextFile) _
			)

	end if

	Dim wfd As New WIN32_FIND_DATA
	Dim handle As Integer
	Dim ret As Integer

	handle = func3( "C:\tmp\*.*", wfd )
	ret = -1

	Do While( handle <> -1 and ret <> 0 )

		Call System.Console.WriteLine( _
			 "{1,-10} {0}", wfd.fileName, wfd.nFileSizeLow )
		ret = func4( handle, wfd )

	Loop


	Call FreeLibrary( hModule )


End Sub

End Module
  



  C# + Framework 2.0

  
using System;
using System.Text;
using System.Runtime.InteropServices;

public delegate int GetSystemDirectory(
	StringBuilder Buff,
	int nSize);
public delegate int GetComputerName(
	StringBuilder Buff,
	ref int nSize);
public delegate int FindFirstFile(
	String Buff,
	[ In, Out ] FindData findFileData);
public delegate int FindNextFile(
	int hFindFile,
	[ In, Out ] FindData findFileData);


public class usedll_01
{

	[DllImport( "Kernel32.dll", CharSet=CharSet.Ansi )]
	public static extern int LoadLibrary(String lpFileName);  

	[DllImport( "Kernel32.dll", CharSet=CharSet.Ansi )]
	public static extern bool FreeLibrary(int hModule);  

	[DllImport( "Kernel32.dll", CharSet=CharSet.Ansi )]
	public static extern IntPtr GetProcAddress(
		int hModule,
		String lpProcName
	);

	public static void Main() {

		int hModule = LoadLibrary("kernel32.dll");

		IntPtr ptr;
		StringBuilder Buffer = new StringBuilder( 512 );
		// **********************************************
		// システムディレクトリ表示
		// **********************************************
		ptr = GetProcAddress(hModule, "GetSystemDirectoryA");
		GetSystemDirectory func1 = 
			(GetSystemDirectory)Marshal.GetDelegateForFunctionPointer(
			ptr,
			typeof(GetSystemDirectory));

		func1( Buffer, 512 );
		Console.WriteLine( "{0}", Buffer );


		// **********************************************
		// PC名表示
		// **********************************************
		ptr = GetProcAddress(hModule, "GetComputerNameA");
		GetComputerName func2 = 
			(GetComputerName)Marshal.GetDelegateForFunctionPointer(
			ptr,
			typeof(GetComputerName));

		int nSize = 512;
		func2( Buffer, ref nSize );
		Console.WriteLine( "{0}", Buffer );

		// **********************************************
		// ファイル一覧
		// **********************************************
		ptr = GetProcAddress(hModule, "FindFirstFileA");
		FindFirstFile func3 = (FindFirstFile)Marshal.GetDelegateForFunctionPointer(
			ptr,
			typeof(FindFirstFile));
		ptr = GetProcAddress(hModule, "FindNextFileA");
		FindNextFile func4 = (FindNextFile)Marshal.GetDelegateForFunctionPointer(
			ptr,
			typeof(FindNextFile));


		FindData wfd = new FindData();
		int handle = func3( "C:\\tmp\\*.*", wfd );
		int ret = -1;

		while( handle != -1 && ret != 0 ) {
			Console.WriteLine( "{1,-10} {0}", 
				wfd.fileName, wfd.nFileSizeLow );
			ret = func4( handle, wfd );
		}

	}

}

[ StructLayout( LayoutKind.Sequential, CharSet=CharSet.Ansi )]
public class FindData 
{
	public int  fileAttributes = 0;
	public int  creationTime_lowDateTime = 0 ;
	public int  creationTime_highDateTime = 0;
	public int  lastAccessTime_lowDateTime = 0;
	public int  lastAccessTime_highDateTime = 0;
	public int  lastWriteTime_lowDateTime = 0;
	public int  lastWriteTime_highDateTime = 0;
	public int  nFileSizeHigh = 0;
	public int  nFileSizeLow = 0;
	public int  dwReserved0 = 0;
	public int  dwReserved1 = 0;
	[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=256 )]
	public String  fileName = null;
	[ MarshalAs( UnmanagedType.ByValTStr, SizeConst=14 )]
	public String  alternateFileName = null;
}
  










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





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

SQLの窓フリーソフト

素材

一般WEBツールリンク

SQLの窓

フリーソフト

JSライブラリ