古いVNCViewer (TightVNC)のせいか、PWを記憶して自動接続ができないので仕方なく作成。
特定のDefaultGateWay IPAddr でのみ動作するので、修正か削除。
ウィンドウハンドル取得 -> コントロールのハンドル取得 -> SendMessage()
文字列送付、ボタン押しを行っています。
////////////////////////////////////////////////////
// VNC AUTO Connect. C++ Ver. Rel1.00.
// (c)inf102 S.H. 2025.
///////////////////////////////////////////////////
#include <iostream>
#include <fstream>
#include <string>
#include <Windows.h>
#include <iphlpapi.h>
#pragma comment(lib, "iphlpapi.lib")
// 文字コード変換 UTF-8 2 UTF-16 (ワイド文字列 Unicode)
std::wstring UTF8ToWString(const std::string& str){
int size_needed = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1,NULL, 0);
std::wstring wstr(size_needed, 0);
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, &wstr[0],size_needed);
// MultiByteToWideChar は末尾に NULL を付けるので最後を削除
if (!wstr.empty() && wstr.back() == L'\0') wstr.pop_back();
return wstr;
}
int main(){
// DefaultGateway 制限
ULONG len = 0;
GetAdaptersInfo(NULL, &len);
PIP_ADAPTER_INFO info = (PIP_ADAPTER_INFO)malloc(len);
if (GetAdaptersInfo(info, &len) == NO_ERROR) {
PIP_ADAPTER_INFO p = info;
printf(" \n ------------------------------------\n");
printf(" VNC ViewerAutoConeect. Relase 1.00.\n");
printf(" (c)inf102 S.H. 2025.\n");
printf(" ------------------------------------\n\n");
printf(" DefaultGateway 172.31.1.254 Only\n");
printf(" IP Address: %s\n",p->IpAddressList.IpAddress.String);
printf(" Default Gateway: %s\n",p->GatewayList.IpAddress.String);
printf(" Adapter Name: %s\n", p->AdapterName);
printf(" Adapter Desc: %s\n", p->Description);
// DEFAULT Gateway
if (strcmp(p->GatewayList.IpAddress.String,"172.31.1.254") != 0) {
printf(" Use GatewayIPAddr: %s\n", p->GatewayList.IpAddress.String);
printf("\r\n Err. NoExec VNC Viewer. DefautGateWayfault.\n\n");
system("pause");
free(info);
return 0;
}
printf(" \n Exec VNC Viewer.\n");
}
else {
printf(" DefaultGateway Get Err. Exit.\n\n");
system("pause");
return 0;
}
free(info);
ShellExecute(NULL, L"open", L"C:\\ProgramFiles\\TightVNC\\tvnviewer.exe", NULL, NULL, SW_SHOWNORMAL);
HWND hWnd;
// Get VNC Viewer hWnd.
while (TRUE) {
hWnd = ::FindWindow(NULL, L"New TightVNC Connection");
Sleep(100);
if (hWnd) break;
}
if (hWnd) {
Sleep(100);
// RemoteHostIPAdder
std::wstring IPAddr = UTF8ToWString ("172.31.1.xxx");
HWND hEdit1 = ::FindWindowEx (hWnd, NULL, L"ComboBox",NULL);
::SendMessageW(hEdit1, WM_SETTEXT, 0,(LPARAM)IPAddr.c_str()); // Unicode (UTF-16)
// Connectボタン(クラス名 "Button")
HWND hButton = ::FindWindowEx(hWnd, NULL, L"Button",L"Connect");
// Connect ボタン押下
if (hButton != NULL) ::SendMessage (hButton, BM_CLICK, 0,0);
}
while (TRUE) {
hWnd = ::FindWindow(NULL, L"Vnc Authentication");
Sleep(100);
if (hWnd) break;
}
std::wstring PW = UTF8ToWString ("xxxxx"); // VNC接続PW
HWND hEdit1 = ::FindWindowEx(hWnd, NULL, L"Edit", NULL);
// 二つ目のEditクラス
HWND hEdit2 = NULL;
if (hEdit1 != NULL) {
// PW欄
hEdit2 = FindWindowEx (hWnd, hEdit1, L"Edit", NULL);
::SendMessageW (hEdit2, WM_SETTEXT, 0,(LPARAM)PW.c_str());
// OK ボタン
HWND hButton = ::FindWindowEx (hWnd, NULL, L"Button",L"OK");
if (hButton != NULL) ::SendMessage (hButton, BM_CLICK, 0,0);
}
return 0;
}