coding code

Posted: March 17, 2011 in 计算机与 Internet

This article will paste some code snippets. You can copy it freely.

C/C++ code:

////////////////////////////////////////////////////////////////////////
SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM) 2); //turn off screen : header:<windows.h>

SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM) -1);//turn on screen :header:<windows.h>

////////////////////////////////////////////////////////////////////////
//run cmd without window: header: <windows.h>

// C void new Proc(char *cmdLine, int show)
void newProc(char *cmdLine, int show = false)
{

STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;

si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = show; //TRUE show window, FALSE no window

BOOL bRet = CreateProcess (
NULL,
cmdLine, // cmdLine
NULL,
NULL,
FALSE,
CREATE_NEW_CONSOLE,
NULL,
NULL,
&si,
&pi);

if(bRet)
{
CloseHandle (pi.hThread);
CloseHandle (pi.hProcess);
}

}

// use it
newProc("notepad demo_file.txt");

////////////////////////////////////////////////////////////////////////
#include <atlcomcli.h>//usecom
#include <shlobj.h>//useIShellLink

#include <Windows.h>
#include <stdio.h>

//#pragma comment(linker,"/subsystem:\"windows\" /entry:\"mainCRTStartup\"") //run without cmd window

//init COM oleaut32.lib ole32.lib
#pragma comment(lib,"ole32.lib")

/**********************************************************************
* Function......: CreateShortcut
* Parameters....: lpszFileName - string that specifies a valid file name
* lpszShortcutPath - string that specifies a path and file name of a shortcut
* lpszArg - string that specifies a argument for a shortcut
* lpszDesc - string that specifies a description for a shortcut
* Returns.......: S_OK on success, error code on failure
* Description...: Creates a Shell link object (shortcut)
**********************************************************************/

BOOL CreateShortcut(/*in*/ LPCTSTR lpszFileName,
/*in*/ LPCTSTR lpszShortcutPath,
/*in*/ LPCTSTR lpszArg,
/*in*/ LPCTSTR lpszDesc,
int i
)
{
CoInitialize(NULL); // init COM interface

HRESULT hRes = E_FAIL;
IShellLink *ipShellLink; // define ISHELLLINK object
WCHAR wszTemp[MAX_PATH]; // Unicode string

hRes = CoCreateInstance(
CLSID_ShellLink,
NULL,
CLSCTX_INPROC_SERVER,
IID_IShellLink,
(LPVOID*)&ipShellLink); // create

if (FAILED (hRes)) // if fail, return
{
return FALSE;
}

IPersistFile *ipPersistFile;// IpersistFile to save ISHELLLINK

hRes = ipShellLink->QueryInterface(IID_IPersistFile, (LPVOID*)&ipPersistFile); // get IpersistFile interface from ISHELLLINK

if (FAILED (hRes)) // if fail, return

{
ipShellLink->Release (); // release ISHELLLINK
return FALSE;
}

hRes = ipShellLink->SetPath(lpszFileName); // shortcut path
hRes = ipShellLink->SetDescription(lpszDesc); // shortcut description

hRes = ipShellLink->SetArguments(lpszArg); // shortcut arguments
ipShellLink->SetIconLocation(_T("C:\\Windows\\System32\\shell32.dll"),i);

// make shure all ANSI string
#if !defined _UNICODE
MultiByteToWideChar(CP_ACP, 0, lpszShortcutPath, -1, wszTemp, MAX_PATH);

#else
wcsncpy(wszTemp, lpszShortcutPath, MAX_PATH);
#endif

hRes = ipPersistFile->Save ((LPWSTR)wszTemp, TRUE); // save

ipPersistFile->Release (); // release IpersistFile

ipShellLink->Release (); // release ISHELLLINK

return TRUE;
}

////////////////////////////////////////////////////////////////////////
/*
* copy myself:
*/

#include <stdio.h>

#include <conio.h>
#include <string.h>
#include <malloc.h>

//#pragma comment(linker,"/subsystem:\"windows\" /entry:\"mainCRTStartup\"") //run without cmd window

int main(int argc,char *argv[])
{
FILE *fp;
FILE *out;

char* name=(char *)malloc(1024* sizeof(char));

// copy ../abc.exe --> ../abcx.exe

strcpy(name,argv[0]);
name=strtok(name,".");
strcat(name,"x.exe");

if((fp=fopen(argv[0],"rb"))==NULL) //open myself

{
printf("open myself error");
return -1;
}

if((out=fopen(name,"wb"))==NULL) //create another

{
printf("open file error");
}

rewind(fp);
for(;!feof(fp);putc(getc(fp),out)); //do clone

fclose(out);
fclose(fp);

return 0;
}

////////////////////////////////////////////////////////////////////////
/*
** auto run next boot
** insert mypath to HKEY_LOCAL_MACHINE SOFTWARE\Microsoft\Windows\CurrentVersion\Run\
*/

#include <windows.h>

#include <string.h>
#include <stdio.h>

#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) // 设置入口地址

#pragma comment(lib,"Advapi32.lib") //advanced api register

int main(int argc,char* argv[])
{
HKEY hKey;//register obj

char *reg;//to insert string
int len=0;//string lenght

reg = (char*) malloc(1024);
//get myself path

//_getcwd(path,_MAX_PATH);
sprintf_s(reg,1024,"\"%s\" -autorun",argv[0]);
for(;reg[len]!=0;len++);
//open register

if(RegOpenKeyEx(
HKEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\\"
,(DWORD)0,KEY_WRITE, &hKey)!=ERROR_SUCCESS)
{
//failure ,return
return -1;
}
//insert

if(RegSetValueExA(hKey,"TestEXE",0,REG_SZ,(const unsigned char *)reg,len)!=ERROR_SUCCESS)
{
//failure ,return

RegCloseKey(hKey);
return -1;
}
RegCloseKey(hKey);

return 0;
}

// Vista/7 need administrator profile

////////////////////////////////////////////////////////////////////////


https://zhengliweb.wordpress.com

Leave a comment