NamedPipe(SafeFileHandle hNamedPipe, int lpOverlapped);
const int PIPE_ACCESS_INBOUND = 0x00000001;
const int PIPE_ACCESS_OUTBOUND = 0x00000002;
const int PIPE_ACCESS_DUPLEX = 0x00000003;
const int PIPE_WAIT = 0x00000000;
const int PIPE_NOWAIT = 0x00000001;
const int PIPE_READMODE_BYTE = 0x00000000;
const int PIPE_READMODE_MESSAGE = 0x00000002;
const int PIPE_TYPE_BYTE = 0x00000000;
const int PIPE_TYPE_MESSAGE = 0x00000004;
const int BUFFER_SIZE = 4096;
const int LINE_LIMIT = 1024;
[DllImport("vncviewer.DLL", EntryPoint = "Init", SetLastError = false,
CharSet = CharSet.Ansi, ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]
public static extern void Init();
[DllImport("vncviewer.DLL", EntryPoint = "Connect", SetLastError = false,
CharSet = CharSet.Ansi, ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]
public static extern int Connect(string hostandport, string password);
private void lstHost_DoubleClick(object sender, EventArgs e)
{
if(lstHost.SelectedItem != null)
Connect(((vncparameter)lstHost.SelectedItem).host, ((vncparameter)lstHost.SelectedItem).password);
}
}
首先添加vncserver地址,然后双击地址表即可通过vncviewer连接到目标主机。
c++代码:
vncviewer.def
LIBRARY "vncviewer"
EXPORTS
Init=Init
Connect=Connect
vncviewer.cxx
BOOL WINAPI DllMain(HINSTANCE,DWORD,LPVOID)
{
return TRUE;
}
void CALLBACK Init()
{
while(!freopen("\\.\pipe\pipeforvncviewer","w",stderr))
Sleep(1000);
// - Initialise the available loggers
initStdIOLoggers();
// - By default, just log errors to stderr
logParams.setDefault("*:stderr:100");
// - Configure the registry configuration reader
win32::RegConfigThread* config = new win32::RegConfigThread();
config->start(HKEY_CURRENT_USER, _T("Software\RealVNC\VNCViewer4"));
// - Tell the rest of VNC Viewer where to write config data to
CConn::userConfigKey.createKey(HKEY_CURRENT_USER, _T("Software\RealVNC\VNCViewer4"));
}
void CALLBACK Connect(const char* hostandport,const char* password)
{
char _hostandport[64];
_hostandport[0] = 0;
char _password[64];
_password[0] = 0;
if(hostandport)
strncpy(_hostandport,hostandport,sizeof(_hostandport));
if(password)
strncpy(_password,password,sizeof(_password));
CConnThread::Connect(_hostandport, _password);
}
logParams.setDefault("*:stderr:100");这里设置100是为了接收所有的输出信息,默认0只接收错误信息。
CConnThread.cxx
void CConnThread::Connect(const char * hostandport, const char * password)
{
CConnThread* connThread;
Lock l(threadsLock);
std::set<CConnThread*>::iterator i;
for(i = threads.begin(); i != threads.end(); i++)
{
connThread = *i;
if(strcmp(connThread->hostOrConfig.buf,hostandport) == 0)
break;
}
if(i == threads.end())
connThread = new CConnThread(hostandport);
}
首先判断指定的ip地址是否已经存在连接,如果不存在则新建连接。
需要注意的地方:
Register.cxx
void RegKey::close() {
if (freeKey) {
//vlog.debug("RegCloseKey