i can't understand why findresource return null. i add new resources and import driver.sys.looked at all possible materials for studying this error, but found nothing to solve it. in all of the cases, zero is returned, what can i do wrong?
MAIN.CPP :
using namespace std;
#pragma warning(disable: 6387)
class Resource
{
public:
struct Parameters
{
std::size_t size_bytes = 0;
void* ptr = nullptr;
};
private:
HRSRC hResource = nullptr;
HGLOBAL hMemory = nullptr;
Parameters p;
public:
Resource(int resource_id, const std::string& resource_class) {
hResource = FindResource(nullptr, MAKEINTRESOURCEA(resource_id), resource_class.c_str());
hMemory = LoadResource(nullptr, hResource);
p.size_bytes = SizeofResource(nullptr, hResource);
p.ptr = LockResource(hMemory);
}
auto GetResourceString() const
{
std::string_view dst;
if (p.ptr != nullptr)
dst = std::string_view(reinterpret_cast<char*>(p.ptr), p.size_bytes);
return dst;
}
};
void GetFile() {
Resource very_important(IDR_DRIVER1, "BINARY");
auto dst = very_important.GetResourceString();
}
int main()
{
Resource res(IDR_DRIVER1, "BINARY");
system("pause");
return 0;
}
RESOURCE.H
//{{NO_DEPENDENCIES}}
// Включаемый файл, созданный в Microsoft Visual C++.
// Используется windowsD resources.rc
//
#define IDR_DRIVER1 101
// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 102
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1001
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif
$(ProjectName).rc :
// Microsoft Visual C++ generated resource script.
//
#include "resource.h"
#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "winres.h"
/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
// Русский (Россия) resources
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_RUS)
LANGUAGE LANG_RUSSIAN, SUBLANG_DEFAULT
#pragma code_page(1251)
#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//
1 TEXTINCLUDE
BEGIN
"resource.h\0"
END
2 TEXTINCLUDE
BEGIN
"#include ""winres.h""\r\n"
"\0"
END
3 TEXTINCLUDE
BEGIN
"\r\n"
"\0"
END
#endif // APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// DRIVER
//
IDR_DRIVER1 DRIVER "driver.sys"
#endif // Русский (Россия) resources
/////////////////////////////////////////////////////////////////////////////
#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//
/////////////////////////////////////////////////////////////////////////////
#endif // not APSTUDIO_INVOKED
IMG folder "driver.sys"/"DRIVER"
You are defining a resource whose ID is 101 and type is DRIVER:
#define IDR_DRIVER1 101
IDR_DRIVER1 DRIVER "driver.sys"
But, you are then asking FindResource() to find a resource whose ID is 101 and type is BINARY rather than DRIVER:
Resource very_important(IDR_DRIVER1, "BINARY");
Resource res(IDR_DRIVER1, "BINARY");
You need to change that to this:
Resource very_important(IDR_DRIVER1, "DRIVER");
Resource res(IDR_DRIVER1, "DRIVER");
Related
I want to display an image from Resource.rc file when I'm using Dear ImGui framework.
I have my code here.
HRSRC resource = FindResourceW(GCM(), MAKEINTRESOURCE(IDB_PNG1), MAKEINTRESOURCE("PGN"));
HANDLE image = LoadResource(GCM(), resource);
int imageWidth = 810;
int imageHeight = 701;
ImGui::Image(image, ImVec2(imageWidth, imageHeight));
inline HMODULE GCM() {
HMODULE hmodule = NULL;
GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCTSTR)GCM, &hmodule);
return hmodule;
}
But when I display my image, it's all black. The data type of HANDLE is void* and the first argument of ImGui::Image want to have the void pointer data type as well.
I have added two files into my Resource.rc file.
The resource.h header file looks like this.
//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by Resource.rc
//
#define IDB_PNG1 101
#define IDI_ICON1 102
// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 103
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1001
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif
And the Resource.rc file looks like this
// Microsoft Visual C++ generated resource script.
//
#include "resource.h"
#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "winres.h"
/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
// Swedish (Sweden) resources
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_SVE)
LANGUAGE LANG_SWEDISH, SUBLANG_SWEDISH
#pragma code_page(1252)
#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//
1 TEXTINCLUDE
BEGIN
"resource.h\0"
END
2 TEXTINCLUDE
BEGIN
"#include ""winres.h""\r\n"
"\0"
END
3 TEXTINCLUDE
BEGIN
"\r\n"
"\0"
END
#endif // APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// PNG
//
IDB_PNG1 PNG "pinmap.png"
/////////////////////////////////////////////////////////////////////////////
//
// Icon
//
// Icon with lowest ID value placed first to ensure application icon
// remains consistent on all systems.
IDI_ICON1 ICON "logo.ico"
#endif // Swedish (Sweden) resources
/////////////////////////////////////////////////////////////////////////////
#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//
/////////////////////////////////////////////////////////////////////////////
#endif // not APSTUDIO_INVOKED
Question:
How can I display an image by loading it from the resources and include that image in ImGui framwork?
The code below compiles and runs. When I debug it, however, the resource is NULL and the size is zero. I imported "Resources\title.display.png" using the wizard, so I have to believe the path to the image is right. No error is thrown. These are the three files that comprise the project. I have wittled it down to this and can't figure out what has gone wrong. Any iseas?
sample.rc:
// Microsoft Visual C++ generated resource script.
//
#include "resource.h"
#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "winres.h"
/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
// English (United States) resources
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
#pragma code_page(1252)
#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//
1 TEXTINCLUDE
BEGIN
"resource.h\0"
END
2 TEXTINCLUDE
BEGIN
"#include ""winres.h""\r\n"
"\0"
END
3 TEXTINCLUDE
BEGIN
"\r\n"
"\0"
END
#endif // APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// PNG
//
IDB_PNG1 PNG "Resources\\title.display.png"
#endif // English (United States) resources
/////////////////////////////////////////////////////////////////////////////
#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//
/////////////////////////////////////////////////////////////////////////////
#endif // not APSTUDIO_INVOKED
resource.h:
//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by sample.rc
//
#define IDB_PNG1 101
// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 102
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1001
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif
sample.cpp:
#include <iostream>
#include <windows.h>
#include "resource.h"
int main(int argc, char** argv)
{
HMODULE module = GetModuleHandle(NULL);
HRSRC resource = FindResource(NULL, MAKEINTRESOURCE(IDB_PNG1), RT_RCDATA);
int resource_size = SizeofResource(module, resource);
return 0;
}
You are storing your image resource as a PNG resource, not an RCDATA resource. As such, when calling FindResource(), you need to change RT_RCDATA to TEXT("PNG"):
HRSRC resource = FindResource(NULL, MAKEINTRESOURCE(IDB_PNG1), TEXT("PNG"));
RT_DATA should have been L"PNG".
I'm trying to add a .wav file as a resource into my C++ game and play it on runtime. Here's my code in my main class:
PlaySound(IDR_WAVE1, GetModuleHandle(NULL), SND_FILENAME);
My resource.h file:
#define IDR_WAVE1 104
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 105
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1001
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif
And my .rc file:
// Microsoft Visual C++ generated resource script.
//
#include "resource.h"
#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "winres.h"
/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
// English (United States) resources
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//
1 TEXTINCLUDE
BEGIN
"resource.h\0"
END
2 TEXTINCLUDE
BEGIN
"#include ""winres.h""\r\n"
"\0"
END
3 TEXTINCLUDE
BEGIN
"\r\n"
"\0"
END
#endif // APSTUDIO_INVOKED
#endif // English (United States) resources
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// English (Australia) resources
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENA)
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_AUS
/////////////////////////////////////////////////////////////////////////////
//
// WAVE
//
IDR_WAVE1 WAVE "C:\\Users\\zjf\\Desktop\\phil.wav"
#endif // English (Australia) resources
/////////////////////////////////////////////////////////////////////////////
#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//
/////////////////////////////////////////////////////////////////////////////
#endif // not APSTUDIO_INVOKED
The issue is, I keep getting an error when trying to play this file from the resource.
argument of type int is incompatible with parameter of type lpcwstr
I've included the resource.h header, and I've tried adding quotation marks in
PlaySound(IDR_WAVE1, GetModuleHandle(NULL), SND_FILENAME);
So it would end up being:
PlaySound("IDR_WAVE1", GetModuleHandle(NULL), SND_FILENAME);
However, this only made the Windows machine beep once.
I would appreciate any help, as I've never worked with resource files.
Instead of:
PlaySound(IDR_WAVE1, GetModuleHandle(NULL), SND_FILENAME);
change to:
PlaySound(MAKEINTRESOURCE(IDR_WAVE1), GetModuleHandle(NULL), SND_RESOURCE);
I'm using SFML with C++ in VS2012. In debug console i have error "failed to load image from memory, no data provided" and "failed to create texture, invalid size <0x0>".
My main.cpp
#include <iostream>
#include "Test.h"
#include "resource.h"
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
using namespace std;
sf::Image LoadImageFromResource(const std::string& name)
{
HRSRC rsrcData = FindResource(NULL, name.c_str(), RT_RCDATA);
DWORD rsrcDataSize = SizeofResource(NULL, rsrcData);
HGLOBAL grsrcData = LoadResource(NULL, rsrcData);
LPVOID firstByte = LockResource(grsrcData);
sf::Image image;
image.loadFromMemory(firstByte, rsrcDataSize);
return image;
}
int main()
{
sf::Image SpriteSheetWalkRes = LoadImageFromResource("IDB_PNG1");
sf::Texture SpriteSheetWalk;
SpriteSheetWalk.loadFromImage(SpriteSheetWalkRes);
}
My Resource.rc
#include "resource.h"
#define APSTUDIO_READONLY_SYMBOLS
#include "afxres.h"
#undef APSTUDIO_READONLY_SYMBOLS
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_RUS)
LANGUAGE LANG_RUSSIAN, SUBLANG_DEFAULT
#ifdef APSTUDIO_INVOKED
1 TEXTINCLUDE
BEGIN
"resource.h\0"
END
2 TEXTINCLUDE
BEGIN
"#include ""afxres.h""\r\n"
"\0"
END
3 TEXTINCLUDE
BEGIN
"\r\n"
"\0"
END
#endif
IDB_PNG1 RCDATA "F:\\(...)\\test_player.png"
#endif
#ifndef APSTUDIO_INVOKED
#endif
My resource.h
#define IDB_PNG1 101
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 102
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1001
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif
I'd suggest using a resource editor or dumpbin.exe and check if your resource was linked into the binary.
I'm a complete noob with Visual Studio, it's probably a stupid mistake.
I'm only trying to instantiate a Cdialog class, I'm making a dll that in the end will need to had some activeX commands in the Window.
I'm tried several ways to instantiate, but I always getting an assertion error.
So in my calldllclass.h I had:
#pragma once
__declspec(dllexport) long init();
in my class that extend Cdialog:
//{{AFX_INCLUDES()
//}}AFX_INCLUDES
class CMyclass: public CDialog{
#include "resource.h"
public:
CMyclass(CWnd* pParent = NULL)
: CDialog(100, pParent){
//{{AFX_DATA_INIT(CMyclass)
//}}AFX_DATA_INIT
}; // standard constructor
virtual ~CMyclass(){};
long calc();
// Dialog Data
//{{AFX_DATA(CMyclass)
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMyclass)
protected:
//}}AFX_VIRTUAL
protected:
// Generated message map functions
//{{AFX_MSG(CMyclass)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//{{AFX_INSERT_LOCATION}}
I created an Resource Class:
#include "resource.h"
#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "afxres.h"
/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
// English (U.S.) resources
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
#ifdef _WIN32
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
#pragma code_page(1252)
#endif //_WIN32
#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//
1 TEXTINCLUDE MOVEABLE PURE
BEGIN
"resource.h\0"
END
2 TEXTINCLUDE MOVEABLE PURE
BEGIN
"#include ""afxres.h""\r\n"
"\0"
END
3 TEXTINCLUDE MOVEABLE PURE
BEGIN
"#define _AFX_NO_SPLITTER_RESOURCES\r\n"
"#define _AFX_NO_OLE_RESOURCES\r\n"
"#define _AFX_NO_TRACKER_RESOURCES\r\n"
"#define _AFX_NO_PROPERTY_RESOURCES\r\n"
"\r\n"
"#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)\r\n"
"#ifdef _WIN32\r\n"
"LANGUAGE 9, 1\r\n"
"#pragma code_page(1252)\r\n"
"#endif //_WIN32\r\n"
"#include ""res\\SimpleVC6SampleEvent.rc2"" // non-Microsoft Visual C++ edited resources\r\n"
"#include ""afxres.rc"" // Standard components\r\n"
"#endif\r\n"
"\0"
END
#endif // APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Dialog
//
IDD_ABOUTBOX DIALOG DISCARDABLE 0, 0, 0, 0
STYLE DS_MODALFRAME
FONT 8, "MS Sans Serif"
BEGIN
END
#endif // English (U.S.) resources
/////////////////////////////////////////////////////////////////////////////
and finally in my cpp I had:
// SIMPLEDIALOGDLL.cpp : Defines the exported functions for the DLL application.
//
#pragma once
#include "stdafx.h"
#include "myclass.h"
#include "resource.h"
#include "dll2export.h"
BEGIN_MESSAGE_MAP(CMyclass, CDialog)
//{{AFX_MSG_MAP(CMyclass)
//}}AFX_MSG
END_MESSAGE_MAP()
long init(){
AfxEnableControlContainer();
CoInitializeEx(NULL,COINIT_MULTITHREADED);
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CMyclass * obj = new CMyclass();
//obj->ShowWindow(SW_HIDE);
obj->ShowWindow(SW_SHOW);
long res = obj->calc();
return (long) obj;
}
long CMyclass::calc(){
return 1+1;
}
When obj->ShowWindow(SW_SHOW); is called I get an
Debug Assertion Failed!
winocc.cpp Line 329
BOOL CWnd::ShowWindow(int nCmdShow)
{
ASSERT(::IsWindow(m_hWnd) || (m_pCtrlSite != NULL));
if (m_pCtrlSite == NULL)
return ::ShowWindow(m_hWnd, nCmdShow);
else
return m_pCtrlSite->ShowWindow(nCmdShow);
}
a little update I put after showWindow it returns 1400
DWORD dw = GetLastError();
add answer sugenstion
I had this to init method
AfxEnableControlContainer();
CoInitializeEx(NULL,COINIT_MULTITHREADED);
AFX_MANAGE_STATE(AfxGetStaticModuleState( ));
CMyclass *obj = new CMyclass();
if(obj->Create(100)){
obj->ShowWindow(SW_HIDE);
DWORD dw = GetLastError();
}else{
DWORD dw = GetLastError();
}
Now I'm having in obj->Create(100)); objcore.cpp Line: 40
#ScottMcP-MVP is right, modeless dialog boxes need a call to the Create method.
Also, according to the MSDN docs on the constructor,
To construct a modeless dialog box, use the protected form of the
CDialog constructor. The constructor is protected because you must
derive your own dialog-box class to implement a modeless dialog box.
Construction of a modeless dialog box is a two-step process. First
call the constructor; then call the Create member function to create a
resource-based dialog box...
So declare your class like CMyclass() : CDialog() {...} and use the following code to display it:
CMyclass * obj = new CMyclass();
if (obj->Create(100))
obj->ShowWindow(SW_SHOW);
else
{ << error >>
}
P.S.: don't forget to delete the variable at the end of your program!
My code was right,
after many many hours, I went to change the compiler options of the Visual Studio and in Debug Information Format put Program Database (/Zi)
That fix it for me