動的 DLL 利用


  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
  










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




SQLの窓  天気  IT用語辞典
Yahoo!ニュース  マルチ辞書
PHP マニュアル  Google URL短縮 


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

SQLの窓フリーソフト

素材

一般WEBツールリンク

SQLの窓

フリーソフト

JSライブラリ