関連ページ  
【VB.NET】dll を COM として登録して VBScript や PHP から使用

01.' **********************************************************  
02.' オブジェクト作成  
03.' **********************************************************  
04.Set obj = CreateObject( "night.toolVb" )  
05.   
06.' **********************************************************  
07.' 7-zip32.dll で 7zip 解凍  
08.' **********************************************************  
09.ret = obj.z7Melt( "test.zip", "melt" )  
10.obj.MsgBox( ret ) 



ブラウザでダウンロード
VBScrip と PHP

どちらも有用なスクリプトですが、Windows のネイティブな処理を行うには、
COM が必要となります。

しかし、VC++ で作成するのは少し敷居が高すぎますし、いろいろ転用する
のには無理があります。

ですから、多少面倒ではありますが、VB.NET で COM を公開します。
ビルド手順
01.; キーペア作成( この処理で作成されたファイルが GUID を持ちます )
02.sn -k lightbox.snk
03. 
04.; いったん、レジストリの登録を解除します
05.; night.dll が登録された場所に存在する場合に有効です
06.; ※ どうしても削除不能になった場合は readme.txt の最後を参照して下さい
07.regasm night.dll /u /tlb:nightLib.tlb
08. 
09.;$OPTION が、参照アセンブリの場所より置き換えられます
10.vbc.exe /target:library $OPTION night.vb
11. 
12.; レジストリに登録して、COM として使用可能にします
13.regasm night.dll /tlb:nightLib.tlb /codebase
14. 
15.; レジストリの情報を出力します( unicode では無いので注意 )
16.; ※ これで出力される場所以外にタイプライブラリにも登録されます
17.regasm night.dll /regfile:night.reg
サンプルコード
通常の Framework の実装は容易です。

7-zip32.dll を使用した解凍処理を内部クラスとして実装して実現しています
001.Imports System
002.Imports System.Reflection
003.Imports System.Windows.Forms
004.Imports System.Runtime.InteropServices
005.Imports System.Text
006. 
007.' CreateObject( "night.toolVb ) で使用します
008.<Assembly: AssemblyKeyFile("lightbox.snk")>
009.Namespace night
010. 
011.    ' "night.toolVb"
012.    Public Class toolVb
013. 
014.        ' ********************************************************
015.        ' 簡単なメソッドのテスト
016.        ' ********************************************************
017.        Public Sub MsgBox( str As String, Optional title As String = "" )
018. 
019.            if title = "" then
020.                MessageBox.Show( str )
021.            else
022.                MessageBox.Show( str, title )
023.            end if
024. 
025.        End Sub
026. 
027.        ' ********************************************************
028.        ' プロパティ
029.        ' ********************************************************
030.        Public Property Clipboard() As String
031.            Get
032.                Return System.Windows.Forms.Clipboard.GetText()
033.            End Get
034.            Set(ByVal value As String)
035.                System.Windows.Forms.Clipboard.SetText(value)
036.            End Set
037.        End Property
038. 
039.        ' ********************************************************
040.        ' 7-zip32.dll で 7zip 解凍
041.        ' ********************************************************
042.        Public Function Z7Melt( zip As String, target as String ) As String
043. 
044.            Dim zip7 As New SevenZip()
045.            Dim szOutput As New System.Text.StringBuilder(500)
046. 
047.            If Not zip7.Load() Then
048.                Return "DLLをロードできません"
049.            End If
050. 
051.            Dim szCommand As String = "x " + zip + " -o" + target
052.            zip7.DoSevenZip(Nothing, szCommand, szOutput, szOutput.Capacity)
053. 
054.            ' 廃棄
055.            zip7.Dispose()
056. 
057.            return szOutput.toString()
058. 
059.        End Function
060. 
061.        Private Class SevenZip
062.         
063.            ' ********************************************************
064.            ' * DLL 内 関数宣言
065.            ' ********************************************************
066.            <DllImport("Kernel32.dll", CharSet:=CharSet.Ansi)> _
067.             Private Shared Function LoadLibrary( _
068.              ByVal lpDllName As String) As IntPtr
069.            End Function
070.         
071.            <DllImport("Kernel32.dll", CharSet:=CharSet.Auto)> _
072.            Private Shared Function FreeLibrary( _
073.             ByVal hModule As IntPtr) As Integer
074.            End Function
075.         
076.            <DllImport("Kernel32.dll", CharSet:=CharSet.Ansi)> _
077.             Private Shared Function GetProcAddress( _
078.              ByVal hModule As IntPtr, _
079.              ByVal lpProcName As String) As IntPtr
080.            End Function
081.         
082.            <DllImport("Kernel32.dll", CharSet:=CharSet.Ansi)> _
083.             Private Shared Function DosDateTimeToFileTime( _
084.              ByVal wFatDate As Short, _
085.              ByVal wFatTime As Short, _
086.              <[In](), Out()> ByVal lpFileTime As FILETIME) As Boolean
087.            End Function
088.         
089.            <DllImport("Kernel32.dll", CharSet:=CharSet.Ansi)> _
090.             Private Shared Function FileTimeToSystemTime( _
091.               <[In](), Out()> ByVal lpFileTime As FILETIME, _
092.               <[In](), Out()> ByVal lpSystemTime As SYSTEMTIME) As Boolean
093.            End Function
094.         
095.            ' ********************************************************
096.            ' * 構造体定義
097.            ' ********************************************************
098.            <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
099.              Public Class INDIVIDUALINFO
100.                Public dwOriginalSize As Integer = 0
101.                Public dwCompressedSize As Integer = 0
102.                Public dwCRC As Integer = 0
103.                Public uFlag As Integer = 0
104.                Public uOSType As Integer = 0
105.                Public wRatio As Short = 0
106.                Public wDate As Short = 0
107.                Public wTime As Short = 0
108.                <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=513)> _
109.                Public szFileName As String = Nothing
110.                <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=3)> _
111.                  Public dummy1 As String = Nothing
112.                <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=8)> _
113.                 Public szAttribute As String = Nothing
114.                <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=8)> _
115.                 Public szMode As String = Nothing
116.            End Class
117.         
118.            <StructLayout(LayoutKind.Sequential)> _
119.              Public Class FILETIME
120.                Public dwLowDateTime As Integer = 0
121.                Public dwHighDateTime As Integer = 0
122.            End Class
123.         
124.            <StructLayout(LayoutKind.Sequential)> _
125.            Public Class SYSTEMTIME
126.                Public wYear As Short = 0
127.                Public wMonth As Short = 0
128.                Public wDayOfWeek As Short = 0
129.                Public wDay As Short = 0
130.                Public wHour As Short = 0
131.                Public wMinute As Short = 0
132.                Public wSecond As Short = 0
133.                Public wMilliseconds As Short = 0
134.            End Class
135.         
136.            ' ********************************************************
137.            '引数
138.            '    _hwnd       7-zip32.dll を呼び出すアプリのウィンドウハンドル。
139.            '    _szFileName 書庫ファイル名。
140.            '    _dwMode     現時点では無視。0 を指定。
141.         
142.            '戻り値
143.            '    指定の書庫ファイルに対応したハンドル。エラー時は NULL
144.            ' ********************************************************
145.            Public Delegate Function SevenZipOpenArchive( _
146.             ByVal hwnd As IntPtr, _
147.             <[In](), MarshalAs(UnmanagedType.LPStr)> ByVal szFileName As String, _
148.             ByVal dwMode As Integer) As IntPtr
149.         
150.            ' ********************************************************
151.            '戻り値
152.            '    正常終了時に 0 、異常時には -1 が返ります。
153.            ' ********************************************************
154.            Public Delegate Function SevenZipCloseArchive( _
155.            ByVal harc As IntPtr) As Integer
156.         
157.            ' ********************************************************
158.            '引数
159.            '    _harc       SevenZipOpenArchive() で返されたハンドル。
160.            '    _szWildName 検索するファイル名の指定。
161.            '                ワイルドカード指定が可能。
162.            '                ファイル名はスペース区切りで複数指定が可能。
163.            '    _lpSubInfo  結果を返すための INDIVIDUALINFO 構造体へのポインタ。
164.            '                結果を必要としない場合は NULL を指定できます。
165.         
166.            '戻り値
167.            '    0           正常終了。
168.            '    -1          検索終了。
169.            '    0,-1 以外  エラー終了。エラーコードを返します。
170.            ' ********************************************************
171.            Public Delegate Function SevenZipFindFirst( _
172.              ByVal harc As IntPtr, _
173.              <[In](), MarshalAs(UnmanagedType.LPStr)> ByVal szWildName As String, _
174.              <[In](), Out()> ByVal lpSubInfo As INDIVIDUALINFO) As Integer
175.         
176.            ' ********************************************************
177.            '引数
178.            '    _harc       SevenZipOpenArchive() で返されたハンドル。
179.            '    _lpSubInfo  結果を返すための INDIVIDUALINFO 構造体へのポインタ。
180.            '                結果を必要としない場合は NULL を指定できます。
181.         
182.            '戻り値
183.            '    0           正常終了。
184.            '    -1          検索終了。
185.            '    0,-1 以外  エラー終了。エラーコードを返します。
186.            ' ********************************************************
187.            Public Delegate Function SevenZipFindNext( _
188.               ByVal harc As IntPtr, _
189.               <[In](), Out()> ByVal lpSubInfo As INDIVIDUALINFO) As Integer
190.         
191.            ' ********************************************************
192.            '_hwnd       7-zip32.dll を呼び出すアプリのウィンドウハンドル。
193.            '            7-zip32.dll は実行時にこのウィンドウに対して
194.            '            EnableWindow() を実行しウィンドウの動作を抑制します。
195.            '            ウィンドウが存在しないコンソールアプリの場合や、
196.            '            指定する必要のない場合は NULL を渡します。
197.            '_szCmdLine  7-zip32.dll に渡すコマンド文字列。
198.            '_szOutput   7-zip32.dll が結果を返すためのバッファ。
199.            '            グローバルメモリー 等の場合はロックされている必要が
200.            '            あります。
201.            '_dwSize     バッファのサイズ。
202.            '            結果が指定サイズを越える場合は、このサイズに
203.            '            切り詰められます。
204.            '            サイズが 1 以上であれば、常に最後に NULL 文字が
205.            '            付加されます。
206.            ' ********************************************************
207.            Public Delegate Function SevenZip( _
208.              ByVal hwnd As IntPtr, _
209.              <[In](), MarshalAs(UnmanagedType.LPStr)> ByVal szCmdLine As String, _
210.              <[In](), Out(), MarshalAs(UnmanagedType.LPStr)> ByVal szOutput As StringBuilder, _
211.              ByVal dwSize As Integer) As Integer
212.         
213.            ' 実行中のチェック
214.            Public Delegate Function SevenZipGetRunning() As Boolean
215.         
216.         
217.            Private hModule As IntPtr
218.            Public OpenArchive As SevenZipOpenArchive
219.            Public CloseArchive As SevenZipCloseArchive
220.            Public FindFirst As SevenZipFindFirst
221.            Public FindNext As SevenZipFindNext
222.            Public GetRunning As SevenZipGetRunning
223.            Public DoSevenZip As SevenZip
224.            Public ft As New FILETIME()
225.            Public st As New SYSTEMTIME()
226.         
227.            ' ********************************************************
228.            ' 7-zip32.dll ロード
229.            ' ********************************************************
230.            Public Function Load() As Boolean
231.         
232.                hModule = LoadLibrary("7-zip32.dll")
233.                If hModule = IntPtr.Zero Then
234.                    Return False
235.                End If
236.         
237.                Dim ptr As IntPtr
238.         
239.                ptr = GetProcAddress(hModule, "SevenZipOpenArchive")
240.                If ptr <> IntPtr.Zero Then
241.                    OpenArchive = _
242.                     Marshal.GetDelegateForFunctionPointer( _
243.                      ptr, _
244.                      GetType(SevenZipOpenArchive) _
245.                     )
246.                End If
247.         
248.                ptr = GetProcAddress(hModule, "SevenZipCloseArchive")
249.                If ptr <> IntPtr.Zero Then
250.                    CloseArchive = _
251.                      Marshal.GetDelegateForFunctionPointer( _
252.                    ptr, _
253.                    GetType(SevenZipCloseArchive) _
254.                     )
255.                End If
256.         
257.                ptr = GetProcAddress(hModule, "SevenZipFindFirst")
258.                If ptr <> IntPtr.Zero Then
259.                    FindFirst = _
260.                      Marshal.GetDelegateForFunctionPointer( _
261.                    ptr, _
262.                    GetType(SevenZipFindFirst) _
263.                     )
264.                End If
265.         
266.                ptr = GetProcAddress(hModule, "SevenZipFindNext")
267.                If ptr <> IntPtr.Zero Then
268.                    FindNext = _
269.                      Marshal.GetDelegateForFunctionPointer( _
270.                    ptr, _
271.                    GetType(SevenZipFindNext) _
272.                     )
273.                End If
274.         
275.                ptr = GetProcAddress(hModule, "SevenZipGetRunning")
276.                If ptr <> IntPtr.Zero Then
277.                    GetRunning = _
278.                    Marshal.GetDelegateForFunctionPointer( _
279.                     ptr, _
280.                     GetType(SevenZipGetRunning) _
281.                   )
282.                End If
283.         
284.                ptr = GetProcAddress(hModule, "SevenZip")
285.                If ptr <> IntPtr.Zero Then
286.                    DoSevenZip = _
287.                    Marshal.GetDelegateForFunctionPointer( _
288.                     ptr, _
289.                     GetType(SevenZip) _
290.                   )
291.                End If
292.         
293.                Return True
294.         
295.            End Function
296.         
297.            ' ********************************************************
298.            ' 日付・時間変換
299.            ' ********************************************************
300.            Public Sub ConvDateTime(ByVal target As INDIVIDUALINFO)
301.         
302.                If hModule <> IntPtr.Zero Then
303.                    DosDateTimeToFileTime(target.wDate, target.wTime, Me.ft)
304.                    FileTimeToSystemTime(Me.ft, Me.st)
305.                End If
306.         
307.            End Sub
308.         
309.            ' ********************************************************
310.            ' 廃棄
311.            ' ********************************************************
312.            Public Sub Dispose()
313.         
314.                If hModule <> IntPtr.Zero Then
315.                    FreeLibrary(hModule)
316.                End If
317.         
318.            End Sub
319.         
320.        End Class
321. 
322.    End Class
323. 
324. 
325.End Namespace
実行サンプル
VBScript と PHP ですが、結果は全く同じになります
01.' **********************************************************
02.' オブジェクト作成
03.' **********************************************************
04.Set obj = CreateObject( "night.toolVb" )
05. 
06.' **********************************************************
07.' MsgBox のテスト
08.' **********************************************************
09.obj.MsgBox( "Optional + 規定値により、タイトルを省略" )
10.Call obj.MsgBox( "全ての引数を指定しています", "タイトル" )
11. 
12. 
13.' **********************************************************
14.' クリップボードのテスト
15.' **********************************************************
16.if obj.Clipboard <> "" then
17.    Call obj.MsgBox( obj.Clipboard, "クリップボード" )
18.end if
19. 
20.obj.Clipboard = "クリップボードへ文字列をコピー"
21. 
22.' **********************************************************
23.' 7-zip32.dll で 7zip 解凍
24.' **********************************************************
25.ret = obj.z7Melt( "test.zip", "melt" )
26.obj.MsgBox( ret )
01.<?
02. 
03.// **********************************************************
04.// オブジェクト作成
05.// **********************************************************
06.$obj = new COM( "night.toolVb" );
07. 
08.// **********************************************************
09.// MsgBox のテスト
10.// **********************************************************
11.$obj->MsgBox( "Optional + 規定値により、タイトルを省略" );
12.$obj->MsgBox( "全ての引数を指定しています", "タイトル" );
13. 
14.// **********************************************************
15.// クリップボードのテスト
16.// **********************************************************
17.if ( $obj->Clipboard != "" ) {
18.    $obj->MsgBox( $obj->Clipboard, "クリップボード" );
19.}
20. 
21.$obj->Clipboard = "クリップボードへ文字列をコピー";
22. 
23. 
24.// **********************************************************
25.// 7-zip32.dll で 7zip 解凍
26.// **********************************************************
27.$ret = $obj->z7Melt( "test.zip", "melt" );
28.$obj->MsgBox( $ret );
29. 
30. 
31.?>