Author Topic: COM with Pelles C  (Read 3180 times)

Vortex

  • Moderator
  • Member
  • *****
  • Posts: 1734
COM with Pelles C
« on: May 11, 2013, 06:39:06 PM »
Here is a Pelles C example with COM. The application extracts the icon of another executable.

Code: [Select]
#include <windows.h>
#include <olectl.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
PICTDESC pd;
HICON hIcon;
HMODULE hModule;
IPicture *pBitmap;
IStream  *pStream;
LONG pcbSize;
HGLOBAL hGlobal;
LPVOID IconAddr;
HANDLE hFile;
DWORD bWritten;

// load the test executable and the associated icon

hModule = LoadLibrary("Test.exe");
hIcon = LoadIcon(hModule,MAKEINTRESOURCE(100));

// initialize the PICTDESC structure

pd.cbSizeofstruct = sizeof(PICTDESC);
pd.picType = PICTYPE_ICON;
pd.icon.hicon = hIcon;

// create the OLE image

OleCreatePictureIndirect(&pd,&IID_IPicture,TRUE,(void **)&pBitmap);

// create the destination stream to save the icon
                                                   
CreateStreamOnHGlobal(NULL,TRUE,(LPSTREAM *)&pStream);

// save the icon to the stream
                                                   
pBitmap->lpVtbl->SaveAsFile(pBitmap,(LPSTREAM)pStream,TRUE,&pcbSize);

// get the address pointing the icon in memory

GetHGlobalFromStream(pStream,&hGlobal);
IconAddr = GlobalLock(hGlobal);

// write the icon to disc

hFile= CreateFile("Smiley.ico",GENERIC_WRITE,0,0,
CREATE_ALWAYS,0,0);

WriteFile(hFile,IconAddr,pcbSize,&bWritten,0);

CloseHandle(hFile);

// release the pointers

pBitmap->lpVtbl->Release(pBitmap);
pStream->lpVtbl->Release(pStream);

return 0;
}

Vortex

  • Moderator
  • Member
  • *****
  • Posts: 1734
Re: COM with Pelles C
« Reply #1 on: May 11, 2013, 08:07:05 PM »
Here is another example modifying the wallpaper.

jj2007

  • Member
  • *****
  • Posts: 7763
  • Assembler is fun ;-)
    • MasmBasic
Re: COM with Pelles C
« Reply #2 on: May 11, 2013, 08:25:55 PM »
Great examples, Erol :t

One thing I like about Pelles C is that it just works. No desperate search for the "right" version of header file xyz, no fighting with obscure error messages, just click the *.ppj, press Control B and voilĂ , "Build OK".

Gunther

  • Member
  • *****
  • Posts: 3518
  • Forgive your enemies, but never forget their names
Re: COM with Pelles C
« Reply #3 on: May 11, 2013, 09:17:09 PM »
Excellent work, Erol.  :t Go forward.

Gunther
Get your facts first, and then you can distort them.