No.1
- 回答日時:
環境変数のCLIENTNAMEか
APIのWTSQuerySessionInformation()
この回答への補足
回答ありがとうございます。
よろしければWTSQuerySessionInformation()の使い方を教えていただけないでしょうか。
No.2ベストアンサー
- 回答日時:
>WTSQuerySessionInformation
OKWEBで「WTSQuerySessionInformation」で検索をかけ、引っかかった2件をベースに、DOBON.NETさんのTipsを見ながら、コンバートしてみました。
http://okweb.jp/kotaeru.php3?q=1409616
http://okweb.jp/kotaeru.php3?q=1419369
http://dobon.net/
私は旧式な人間なため、VB6感覚で作成したために、内部で文字コード変換を行っております。
おそらくAPIの宣言次第では、コンバート部は不要だと思うのですが、調べるのが面倒なので、そのまま載せちゃいます。
それと
http://www.microsoft.com/japan/msdn/library/defa …
のWTSInfoClassのデータの形式を見ると、
「関数をターミナルサービスコンソールから呼び出すと、NULL ポインタを受け取ります」
とあります。
私のところでは、ターミナルサービスコンソールから呼び出す実験をしておりません。
だから、結果が正しいかどうかは無視として、とりあえずコンバートはこんな感じかなぁっていうところです。
[モジュール]Module1.vb
[クラス]WTS.vb
の構成です。
-----------------------------------------------
Module Module1
Sub Main()
Dim objWTS As New WTS()
Dim strWk As String
If objWTS.Get_WTSQuerySessionInformation(WTS.WTS_INFO_CLASS.WTSUserName, strWk) Then
MsgBox(strWk, MsgBoxStyle.Information)
Else
MsgBox("失敗", MsgBoxStyle.Critical)
End If
objWTS = Nothing
End Sub
End Module
-----------------------------------------------
Imports System.Text
Imports System.Runtime.InteropServices
Public Class WTS
Public Enum WTS_INFO_CLASS
WTSInitialProgram
WTSApplicationName
WTSWorkingDirectory
WTSOEMId
WTSSessionId
WTSUserName
WTSWinStationName
WTSDomainName
WTSConnectState
WTSClientBuildNumber
WTSClientName
WTSClientDirectory
WTSClientProductId
WTSClientHardwareId
WTSClientAddress
WTSClientDisplay
WTSClientProtocolType
End Enum
<DllImport("kernel32.dll")> _
Private Shared Function lstrlen( _
ByVal Ptr As Integer _
) As Integer
End Function
<DllImport("kernel32.dll")> _
Private Shared Function lstrcpy( _
ByVal lpString1 As Integer _
, ByVal lpString2 As Integer _
) As Integer
End Function
<DllImport("wtsapi32.dll")> _
Private Shared Function WTSOpenServer( _
ByVal pServerName As Integer _
) As Integer
End Function
<DllImport("wtsapi32.dll")> _
Private Shared Sub WTSFreeMemory( _
ByRef pMemory As Integer _
)
End Sub
<DllImport("wtsapi32.dll")> _
Private Shared Function WTSQuerySessionInformation( _
ByVal hServer As Integer _
, ByVal SessionId As Integer _
, ByVal WTSInfoClass As WTS_INFO_CLASS _
, ByRef ppBuffer As Integer _
, ByRef pBytesReturned As Integer _
) As Boolean
End Function
Public Function Get_WTSQuerySessionInformation(ByVal inWTSInfoClass As WTS_INFO_CLASS, ByRef 取得文字列 As String) As Boolean
Dim l_strRet As String = ""
Dim hServer As Integer
Dim blnRet As Boolean
Dim dwBytesReturned As Integer
Dim lpBuffer As Integer
Const WTS_CURRENT_SESSION As Integer = 0
hServer = WTSOpenServer(0)
blnRet = WTSQuerySessionInformation(hServer, WTS_CURRENT_SESSION, inWTSInfoClass, lpBuffer, dwBytesReturned)
If blnRet Then
l_strRet = GetStringFromPointer(lpBuffer)
Call WTSFreeMemory(lpBuffer)
End If
取得文字列 = l_strRet
Return blnRet
End Function
'*********************************************
'** ポインタから文字列取得
'*********************************************
Private Function GetStringFromPointer(ByVal inPointer As Integer) As String
Dim l_strRet As String = ""
'文字長取得
Dim l_intLen As Integer = lstrlen(inPointer)
If (l_intLen > 0) Then
Dim bytAry(l_intLen - 1) As Byte
Dim gch As GCHandle = GCHandle.Alloc(bytAry, GCHandleType.Pinned)
Dim address As Integer = gch.AddrOfPinnedObject().ToInt32()
Call lstrcpy(address, inPointer)
gch.Free()
Call Conv_UTF8_to_SJIS(bytAry, l_strRet)
End If
Return l_strRet
End Function
'*********************************************
'** エンコード変換
'*********************************************
Private Sub Conv_UTF8_to_SJIS(ByVal inByte() As Byte, ByRef otStr As String)
Dim strSJIS As Encoding = Encoding.GetEncoding("Shift-JIS")
Dim strUTF8 As Encoding = Encoding.UTF8
Dim asciiBytes As Byte() = Encoding.Convert(strUTF8, strSJIS, inByte)
Dim chrSJIS(strSJIS.GetCharCount(asciiBytes, 0, asciiBytes.Length - 1)) As Char
strSJIS.GetChars(asciiBytes, 0, asciiBytes.Length, chrSJIS, 0)
otStr = New String(chrSJIS)
End Sub
End Class
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
関連するカテゴリからQ&Aを探す
おすすめ情報
- ・漫画をレンタルでお得に読める!
- ・人生のプチ美学を教えてください!!
- ・10秒目をつむったら…
- ・あなたの習慣について教えてください!!
- ・牛、豚、鶏、どれか一つ食べられなくなるとしたら?
- ・【大喜利】【投稿~9/18】 おとぎ話『桃太郎』の知られざるエピソード
- ・街中で見かけて「グッときた人」の思い出
- ・「一気に最後まで読んだ」本、教えて下さい!
- ・幼稚園時代「何組」でしたか?
- ・激凹みから立ち直る方法
- ・1つだけ過去を変えられるとしたら?
- ・【あるあるbot連動企画】あるあるbotに投稿したけど採用されなかったあるある募集
- ・【あるあるbot連動企画】フォロワー20万人のアカウントであなたのあるあるを披露してみませんか?
- ・映画のエンドロール観る派?観ない派?
- ・海外旅行から帰ってきたら、まず何を食べる?
- ・誕生日にもらった意外なもの
- ・天使と悪魔選手権
- ・ちょっと先の未来クイズ第2問
- ・【大喜利】【投稿~9/7】 ロボットの住む世界で流行ってる罰ゲームとは?
- ・推しミネラルウォーターはありますか?
- ・都道府県穴埋めゲーム
- ・この人頭いいなと思ったエピソード
- ・準・究極の選択
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
エクセル タブの下のメニューを...
-
Web画面の文字をVB6で取得したい
-
Outlookの「受信日時」「件名」...
-
ExcelVBA シート名を複数セルか...
-
VBA 入力箇所指定方法
-
Excel VBA 定義されたプロージ...
-
VBA 別ブックからコピペしたい...
-
エクセルのマクロついて教えて...
-
VBA ユーザーフォーム ボタンク...
-
【ExcelVBA】インデックスが有...
-
配列のペースト出力結果の書式...
-
VBA実行後に元のセルに戻りたい
-
エクセルのマクロについて教え...
-
VBA 別ブックから条件に合うも...
-
エクセルのマクロについて教え...
-
エクセルのマクロについて教え...
-
IF文、条件分岐の整理方法
-
VBA listBoxから
-
VBAの質問になります 行の非表示
-
【VBA】カーソルのある行の1行...
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
byte型をstring型として扱うには
-
VB.NETで変数の宣言が上手くで...
-
VBのメモリのアドレス取得
-
ターミナルサービスでのクライ...
-
vba 時間の引き算 例えば 15:00...
-
Designer.vbは直接コードをいじ...
-
「読み込み違反」が起きたとき...
-
意味不明の実行時エラーで困っ...
-
Visual Studio 「AnyCpu」について
-
excel vbaから実行するexe実行...
-
印刷要求順番と印刷出力順番が...
-
印刷ダイアログを表示させない方法
-
VBAでOutlookを終了させたい Ex...
-
Windows上のプログラム。「予め...
-
VB6(SP5)+OO4OでCreateObjectが...
-
VBAでArrayListを使う為の「msc...
-
ManagementClassが見つからない。
-
アウトルックが起動しているか...
-
VBSで起動したアプリが前面表示...
-
VBSから別のVBS起動するとき変...
おすすめ情報