简体   繁体   中英

Use relative file path with LoadImage

I am attempting to load a file from a relative file path in win32 via the LoadImage function. The URL I am using definitely exists as I am, for testing purposes, using the same URL as an added bitmap in the resource file.

Image = (HBITMAP)LoadImage(NULL, "..\\..\\Images\\Mage default.bmp",
                           IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);

However, it is returning error code 2, indicating the file can't be found. I have googled the issue at some length, and referred to MSDN, and I can't find anything stating how to use a relative file path with LoadImage .

It seems to work fine with LoadBitmap and a pre-defined bitmap, but doesn't seem to work in this case. Any help would be much appreciated

First of all, check that the file exist and current directory is the one you expected.

Also, you can use function like GetFullPathName to convert relative path to absolute.

您拥有的相对路径是根据进程/运行时工作目录而不是编译时的静态文件位置计算的。

This may be an old post, but having stumbled across this problem (in my case, a header file) and found an answer, here it is:

If referencing a relative path, the first directory is not preceeded by a backslash.

In this example, the line would be:

(HBITMAP)LoadImage(NULL,"Images\\\\Mage default.bmp",...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM