- PR -

VB.netでのCopyMemoryを利用するには

1
投稿者投稿内容
マー坊
会議室デビュー日: 2005/04/27
投稿数: 6
投稿日時: 2005-06-01 20:55
下記のようなコードがあります。
これはターミナルサービスに現在のセッション情報を取得し
デバック情報として表示するものです。
(microsoft社のサンプルより)


Private Type WTS_SESSION_INFO
SessionID As Long
pWinStationName As Long
state As WTS_CONNECTSTATE_CLASS
End Type

Private Declare Function WTSEnumerateSessions _
Lib "wtsapi32.dll" Alias "WTSEnumerateSessionsA" ( _
ByVal hServer As Long, ByVal Reserved As Long, _
ByVal Version As Long, ByRef ppSessionInfo As Long, _
ByRef pCount As Long _
) As Long

Private Declare Sub WTSFreeMemory Lib "wtsapi32.dll" ( _
ByVal pMemory As Long)

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
Destination As Any, Source As Any, ByVal length As Long)

Private Function GetWTSSessions() As WTS_SESSION_INFO()
Dim RetVal As Long
Dim lpBuffer As Long
Dim Count As Long
Dim p As Long
Dim arrSessionInfo() As WTS_SESSION_INFO
Dim i As Integer


RetVal = WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, _
0&, _
1, _
lpBuffer, _
Count)
If RetVal Then
' WTSEnumerateProcesses was successful.

p = lpBuffer
ReDim arrSessionInfo(Count - 1)


問題箇所
CopyMemory arrSessionInfo(0), ByVal p,Count * LenB(arrSessionInfo(0))


For i = LBound(arrWTSSessions) To UBound(arrWTSSessions)

Debug.Print "Session ID: " & arrWTSSessions(i).SessionID
Debug.Print "Machine Name: " & _
PointerToStringA(arrWTSSessions(i).pWinStationName)
Debug.Print "Connect State: " & arrWTSSessions(i).state
Debug.Print "***********"

Next i





しかしこれはVB6用のもののためVB.netにコンバートしようとしたのですがうまくいきません。

問題点
1.問題となっているのはWTSEnumerateSessionsの4番目の引数(lpBuffer)を
WTS_SESSION_INFO型の構造体にコピーしているCopyMemory(API)がVB.netでは
うまくいきません。


ご存知の方どうかお力添えをおねがいします。

Hongliang
ぬし
会議室デビュー日: 2004/12/25
投稿数: 576
投稿日時: 2005-06-02 01:13
Declare宣言中のppSessionInfoパラメータを、ByRef ppSessionInfo As System.IntPtrと宣言し、

コード:
Dim buffer As System.IntPtr
Dim count As Integer
Dim result As Boolean  'WTSEnmerateSessionsの返値はBooleanで宣言中
result = WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, 0, 1, buffer, count)

If result Then
  Dim i As Integer
  Dim wts = GetType(WTS_SESSION_INFO)
  Dim structSize As Integer = System.Runtime.InteropServices.Marshal.SizeOf(wts)
  For i = 0 To count - 1
    '開始位置から(構造体のサイズ*現在位置)ぶん進めたポインタを取得
    Dim current As New System.IntPtr(buffer.ToInt64() + (structSize * i))
    Dim obj As Object = System.Runtime.InteropServices.Marshal.PtrToStructure(current, wts)
    Dim info As WTS_SESSION_INFO = CType(obj, WTS_SESSION_INFO)

    Debug.WriteLine(info.SessionID)
  Next
End If


と、ポインタをずらしていって一つ一つ構造体にしていくのがいいんではないでしょうか。
マー坊
会議室デビュー日: 2005/04/27
投稿数: 6
投稿日時: 2005-06-03 21:57
解決できました ほんとありがとうございます
1

スキルアップ/キャリアアップ(JOB@IT)