1,创建进程
#include
<
windows.h
>
#include
<
stdio.h
>

int
main(VOID)
{
STARTUPINFOsi;
PROCESS_INFORMATIONpi;
ZeroMemory(&si,sizeof(si));
si.cb=sizeof(si);
ZeroMemory(&pi,sizeof(pi));
//Startthechildprocess.
if(!CreateProcess(NULL,//Nomodulename(usecommandline).
"C://WINDOWS//system32//mspaint.exe",//Commandline.
NULL,//Processhandlenotinheritable.
NULL,//Threadhandlenotinheritable.
FALSE,//SethandleinheritancetoFALSE.
0,//Nocreationflags.
NULL,//Useparent'senvironmentblock.
NULL,//Useparent'sstartingdirectory.
&si,//PointertoSTARTUPINFOstructure.
&pi)//PointertoPROCESS_INFORMATIONstructure.
)

{
printf("CreateProcessfailed(%d)./n",GetLastError());
return-1;
}
//Waituntilchildprocessexits.
WaitForSingleObject(pi.hProcess,INFINITE);
//Closeprocessandthreadhandles.
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
2,创建线程
#include
<
stdio.h
>
#include
<
iostream
>
#include
<
windows.h
>
using
namespace
std;

DWORDSum;
/**/
/*dataissharedbythethread(s)*/


/**/
/*thethreadrunsinthisseparatefunction*/
DWORDWINAPISummation(PVOIDParam)
{
DWORDUpper=*(DWORD*)Param;
for(DWORDi=0;i<=Upper;i++)
Sum+=i;
return0;
}

int
main(
int
argc,
char
*
argv[])
{
DWORDThreadId;
HANDLEThreadHandle;
intParam=100;
if(Param<0)

{
fprintf(stderr,"aninteger>=0isrequired/n");
return-1;
}
//createthethread
ThreadHandle=CreateThread(NULL,0,Summation,&Param,0,&ThreadId);
if(ThreadHandle!=NULL)
{
WaitForSingleObject(ThreadHandle,INFINITE);
CloseHandle(ThreadHandle);
printf("sum=%d/n",Sum);
}
}
3,加载BMP文件
#include
<
windows.h
>
#include
<
fstream
>
using
namespace
std;
char
szAppName[]
=
"
BMPLoad
"
;
LRESULTCALLBACKWindowProc(HWND,UINT,WPARAM,LPARAM);
//
**********
//
classCRaster
//
-GenericclassforBMPrasterimages.

class
CRaster
{
public:
intWidth,Height;//Dimensions
intBPP;//BitsPerPixel.
char*Raster;//BitsoftheImage.
RGBQUAD*Palette;//RGBPalettefortheimage.
intBytesPerRow;//RowWidth(inbytes).
BITMAPINFO*pbmi;//BITMAPINFOstructure
//Memberfunctions(definedlater):
intLoadBMP(char*szFile);
intGDIPaint(HDChdc,intx,inty);
}
;
//
**********
//
WindowsMainFunction.
//
-Herestartsourdemoprogram
int
WINAPIWinMain(HINSTANCEhInstance,HINSTANCEhPrevInstance,
LPSTRlpCmdLine,
int
nCmdShow)
{
HWNDhwnd;
MSGmsg;
WNDCLASSwc;
wc.style=CS_HREDRAW|CS_VREDRAW;
wc.lpfnWndProc=WindowProc;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hInstance=hInstance;
wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName=NULL;
wc.lpszClassName=szAppName;
RegisterClass(&wc);
hwnd=CreateWindow(szAppName,"BMPLoad",WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
0,0,hInstance,0);
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg,0,0,0))

{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
returnmsg.wParam;
}

//
**********
//
MainWindowProcedure.
//
-ProcessesWindowMessages
LRESULTCALLBACKWindowProc
(HWNDhwnd,UINTmessage,WPARAMwParam,LPARAMlParam)
{
staticCRasterbmp;
HDChdc;
PAINTSTRUCTps;
switch(message)

{
caseWM_CREATE:
bmp.LoadBMP("example.bmp");
return0;
caseWM_PAINT:
hdc=BeginPaint(hwnd,&ps);
bmp.GDIPaint(hdc,10,10);
EndPaint(hwnd,&ps);
return0;
caseWM_DESTROY:
PostQuitMessage(0);
return0;
}
returnDefWindowProc(hwnd,message,wParam,lParam);
}

//
**********
//
CRaster::LoadBMPFile(FileName);
//
-loadsaBMPfileintoaCRasterobject
//
*supportsnon-RLE-compressedfilesof1,2,4,8&24bits-per-pixel
int
CRaster::LoadBMP(
char
*
szFile)
{
BITMAPFILEHEADERbmfh;
BITMAPINFOHEADERbmih;
//Openfile.
ifstreambmpfile(szFile,ios::in|ios::binary);
if(!bmpfile.is_open())return1;//Erroropeningfile
//Loadbitmapfileheader&infoheader
bmpfile.read((char*)&bmfh,sizeof(BITMAPFILEHEADER));
bmpfile.read((char*)&bmih,sizeof(BITMAPINFOHEADER));
//Checkfiletypesignature
if(bmfh.bfType!='MB')return2;//FileisnotBMP
//Assignsomeshortvariables:
BPP=bmih.biBitCount;
Width=bmih.biWidth;
Height=(bmih.biHeight>0)?bmih.biHeight:-bmih.biHeight;//absoultevalue
BytesPerRow=Width*BPP/8;
BytesPerRow+=(4-BytesPerRow%4)%4;//intalignment
//IfBPParen't24,loadPalette:
if(BPP==24)pbmi=(BITMAPINFO*)newchar[sizeof(BITMAPINFO)];
else

{
pbmi=(BITMAPINFO*)newchar[sizeof(BITMAPINFOHEADER)+(1<<BPP)*sizeof(RGBQUAD)];
Palette=(RGBQUAD*)((char*)pbmi+sizeof(BITMAPINFOHEADER));
bmpfile.read((char*)Palette,sizeof(RGBQUAD)*(1<<BPP));
}
pbmi->bmiHeader=bmih;
//LoadRaster
bmpfile.seekg(bmfh.bfOffBits,ios::beg);
Raster=newchar[BytesPerRow*Height];
//(ifheightispositivethebmpisbottom-up,readitreversed)
if(bmih.biHeight>0)
for(intn=Height-1;n>=0;n--)
bmpfile.read(Raster+BytesPerRow*n,BytesPerRow);
else
bmpfile.read(Raster,BytesPerRow*Height);
//so,wealwayshaveaup-bottomraster(thatisnegativeheightforwindows):
pbmi->bmiHeader.biHeight=-Height;
bmpfile.close();
return0;
}

//
**********
//
CRaster::GDIPaint(hdc,x,y);
//
*PaintsRastertoaWindowsDC.
int
CRaster::GDIPaint(HDChdc,
int
x
=
0
,
int
y
=
0
)
{
//Painttheimagetothedevice.
returnSetDIBitsToDevice(hdc,x,y,Width,Height,0,0,
0,Height,(LPVOID)Raster,pbmi,0);
}

本文介绍了如何在Windows环境下使用C/C++创建进程与线程,并展示了加载BMP图像文件的具体实现。首先通过CreateProcess函数启动外部程序,接着演示了通过CreateThread函数创建线程并进行求和运算的过程,最后详细说明了BMP文件的加载方法。
&spm=1001.2101.3001.5002&articleId=81548421&d=1&t=3&u=32a47428c2904b9483dce4f3d3ff634a)
152

被折叠的 条评论
为什么被折叠?



