Why doesn't this work?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Why doesn't this work?

  1. #1
    Join Date
    Nov 2003
    Location
    Bacau, Romania
    Posts
    474

    Question Why doesn't this work?

    Code:
    HICON hIcon;
    LPBYTE lpBuffer;
    
    hIcon=dlg.GetIconHandle();
    
    lpBuffer=(LPBYTE)::GlobalLock(hIcon);
    
    HANDLE hUpd=BeginUpdateResource("D:\\n.exe",0);
    UpdateResource((HMODULE)hUpd,
     RT_ICON,MAKEINTRESOURCE(1),
     MAKELANGID(LANG_NEUTRAL,
     SUBLANG_DEFAULT),
     (LPBYTE)lpBuffer,
     GlobalSize(hIcon));
    EndUpdateResource(hUpd, FALSE);
    
    GlobalUnlock(lpBuffer);
    I'm using this code to take the icon from an exe and replace the icon in another exe, but it doesn't work. Every time lpBuffer is empty after GlobalLock. the hIcon handle is retrieved by ExtractIcon();
    What am I doing wrong?
    Please help!
    B.A. (NKProds)

    Here is what I do: http://www.nkprods.com
    I couldn't have done it without your help. Thanks.

  2. #2
    Join Date
    Aug 2004
    Location
    Bucharest, Romania... sometimes
    Posts
    1,039

    Re: Why doesn't this work?

    http://www.crispen.org/src/archive/0005.html
    Not sure if this code works because I didn't try it myself, but it seems you have to make your own implementation that converts from HICON to raw data pointer used in a UpdateResource() call.

    Also, it seems that LockResource() should be used instead of GlobalLock().

    If you want a sample of how icon data must be provided for exe update, search the following link for "UpdateResource(RT_ICON":
    http://cvs.sourceforge.net/viewcvs.p...l.cpp?rev=1.10

    Best of luck,
    Bogdan Apostol
    ESRI Developer Network

    Compilers demystified - Function pointers in Visual Basic 6.0
    Enables the use of function pointers in VB6 and shows how to embed native code in a VB application.

    Customize your R2H
    The unofficial board dedicated to ASUS R2H UMPC owners.

  3. #3
    Join Date
    Nov 2003
    Location
    Bacau, Romania
    Posts
    474

    Re: Why doesn't this work?

    The link http://www.crispen.org/src/archive/0005.html doesn't work!
    I've tried www.crispen.org too but the same result: Could not connect
    B.A. (NKProds)

    Here is what I do: http://www.nkprods.com
    I couldn't have done it without your help. Thanks.

  4. #4
    Join Date
    Aug 2004
    Location
    Bucharest, Romania... sometimes
    Posts
    1,039

    Re: Why doesn't this work?

    Hi nemok!
    Please find the attachment containing the web page from the link:
    http://www.crispen.org/src/archive/0005.html
    Then again, I think the second link I've posted was better.
    It seems that many before had the same problem you're facing.
    Probably, many again will, so, it is a good thing for you to post a FAQ when will have a solution.

    Thanks,
    Bogdan Apostol
    ESRI Developer Network

    Compilers demystified - Function pointers in Visual Basic 6.0
    Enables the use of function pointers in VB6 and shows how to embed native code in a VB application.

    Customize your R2H
    The unofficial board dedicated to ASUS R2H UMPC owners.

  5. #5
    Join Date
    Nov 2003
    Location
    Bacau, Romania
    Posts
    474

    Re: Why doesn't this work?

    The links you provided me with show only how to read an icon from an .ico file, which is not what I need.
    I found the following code in MSDN (Q104570) on howw to save an HICON to disk and then read it and so gain an pointer to the icon.

    Code:
    int NEAR _pascal SaveMyIcon( HICON hIcon)
    {
      int       fh, i, iResult;
      UINT      uiSize;
      DWORD     dwSize;
      OFSTRUCT  of;    if (!hIcon)
          return FALSE;    dwSize  = GlobalSize(hIcon);
        lpGMem  = GlobalLock(hIcon);
        fh = OpenFile ("myicon.bin", &of, OF_WRITE | OF_CREATE);    if (fh == -1)  // If NOT opened successfully.
        {
         MessageBox(NULL, "Unable to create file", NULL, MB_OK );
         return FALSE;
        }    uiSize = _lwrite(fh, (LPSTR)lpGMem, (UINT)dwSize);
        _lclose(fh);    if (uiSize == -1 || uiSize < (UINT)dwSize)
        {
           MessageBox(NULL, "Unable to read file", NULL, MB_OK );
           return FALSE;
        }
        else // Everything worked, return hGMem.
        {
          return (HICON)hGMem;
        }
    }
    The problem is that it doesn't work. The lpGMem pointer is always NULL.
    Is it might be because I don't know what type lpGMem is (the article doesn't say). I made it LPTSTR, LPBYTE... and it doesnt work. What sould I do? Is it really not posible to gain a valid pointer from an HICON handle provided by ExtractIcon()?
    Please help!
    B.A. (NKProds)

    Here is what I do: http://www.nkprods.com
    I couldn't have done it without your help. Thanks.

  6. #6
    Join Date
    Nov 2003
    Location
    Bacau, Romania
    Posts
    474

    Re: Why doesn't this work?

    anyone?Please!
    B.A. (NKProds)

    Here is what I do: http://www.nkprods.com
    I couldn't have done it without your help. Thanks.

  7. #7
    Join Date
    Nov 2003
    Location
    Bacau, Romania
    Posts
    474

    Re: Why doesn't this work?

    ???????????????????
    B.A. (NKProds)

    Here is what I do: http://www.nkprods.com
    I couldn't have done it without your help. Thanks.

  8. #8
    Join Date
    Aug 2004
    Location
    Bucharest, Romania... sometimes
    Posts
    1,039

    Re: Why doesn't this work?

    I found a software doing what you want to do, so it is possible
    http://www.softboy.net/exeico/
    I've seen another sample code using a different function to extract an icon from a file, and maybe you want to try it out:
    Code:
    SHGetFileInfo("c:\\windows\\notepad.exe",0,&stFileInfo,sizeof(stFileInfo),SHGFI_ICON);
    pDC->DrawIcon(10,10,stFileInfo.hIcon);
    Since you've said before that drawing the icon obtained with ExtractIcon() works, but LockResource() or GlobalLock() are always returning NULL, makes me believe that ExtractIcon() doesn't fail, so there's no need to change it.
    I have another question though:
    Do you use LoadLibrary() to load the module from where you need to copy your icon?
    http://msdn.microsoft.com/library/de...gResources.asp
    Quote Originally Posted by MSDN
    The pointer returned by LockResource is valid until the module containing the resource is unloaded. It is not necessary to unlock resources because the system automatically deletes them when the process that created them terminates.

    Do not try to lock a resource by using the handle returned by the FindResource or FindResourceEx function. Such a handle points to random data.
    These are the remarks on LockResource() function.

    I'm so sorry I couldn't make the time to write a fully functional sample code, but I hope these links will lead you to it.

    Best regards,
    Bogdan Apostol
    ESRI Developer Network

    Compilers demystified - Function pointers in Visual Basic 6.0
    Enables the use of function pointers in VB6 and shows how to embed native code in a VB application.

    Customize your R2H
    The unofficial board dedicated to ASUS R2H UMPC owners.

  9. #9
    Join Date
    Feb 2010
    Posts
    24

    Re: Why doesn't this work?

    Code:
    int NEAR  SaveMyIcon( HICON hIcon)
    {
    	int       fh, i, iResult;
    	UINT      uiSize;
    	DWORD     dwSize;
    	OFSTRUCT  of;    if (!hIcon)
    		return FALSE;    
    	dwSize  = GlobalSize(hIcon);
    	void* lpGMem;
    	lpGMem = GlobalLock(hIcon);
    	fh = OpenFile ("myicon.bin", &of, OF_WRITE | OF_CREATE);    if (fh == -1)  // If NOT opened successfully.
    	{
    		MessageBox(NULL, "Unable to create file", NULL, MB_OK );
    		return FALSE;
    	}    uiSize = _lwrite(fh, (LPSTR)lpGMem, (UINT)dwSize);
    	_lclose(fh);    if (uiSize == -1 || uiSize < (UINT)dwSize)
    	{
    		MessageBox(NULL, "Unable to read file", NULL, MB_OK );
    		return FALSE;
    	}
    }

    why error

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center