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;
}