#pragma section and attributes - visual-c++

Just trying to make a new section and setting up his attributes with #pragma return this warning:
warning C4330: attribute 'write' for section '.mysec' ignored
Simple code:
#include <windows.h>
#include <stdio.h>
#pragma section(".mysec",execute,read,write)
__declspec(allocate(".mysec")) UCHAR var[] = {0xDE, 0xAD, 0xBE, 0xEF};
void main() { return; }
linker options: /DYNAMICBASE:NO, /FIXED, /NXCOMPAT:NO, /OPT:NOREF
OS/tools: Win x64 / msvc++ 110
I read some articles on MSDN and in particulary this http://msdn.microsoft.com/en-us/library/50bewfwa(v=vs.110).aspx but didn't found answer.
Thanks.

I think that this due to the execute flag. I don't think you can have a section that contains writeable code in Windows.
I might be remembering this wrong but it would a security issue and thus not supported.

Related

Dynamic Linker does not resolve symbol although the library is already loaded

I stumbled over the following problem in my large-grown project: I have a set of libraries which depend on each other and on external libraries. Of one dependency ("libvtkCommonCore-*.so"), there are different variants, which need to be used interchangeably. The variants have different suffixes ("libvtkCommonCore-custom1.so", "libvtkCommonCore-custom2.so" and so on). Thus I cannot link the library, which needs symbols from it, directly to the providing library. Rather I link the application of the library which uses it to the appropriate variant and then load my own library.
This approach generally works but fails under some circumstances and I'm a bit lost while finding out what goes wrong.
This situation is working:
Sketch of situation 1
("libA" needs symbols from "libvtkCommonCore". It is loaded at run time by the constructor of some static object in "libB" using a "dlopen" call with flags RTLD_LAZY|RTLD_GLOBAL. libvtkCommonCore* and libB were linked at build time to an executable)
This situation now ceases to work:
Sketch of situation 2
(actually the same as before but complicated by the fact that libvtkCommonCore* and libB are linked to another library libC at build time. This library is loaded from an executable at run time using "dlopen")
I investigated the case by setting LD_DEBUG to "files", "symbols" and/or "binding" and study the output. It reveals that libvtkCommonCore* is loaded, initialized and kept in memory all the time and before libA is loaded. When the linked tries to resolve "SymbolX" in libA, it does not search libvtkCommonCore, although it did for other libraries which needed the same symbol.
Note: I use Linux (Ubuntu 20) with the recent Gcc and CMake. Both the executable in situation 1 and "libC" in situation 2 were built with the flags "-Wl,--add-needed -Wl,--no-as-needed".
Note 2: if I launch the executable in situation 2 with LD_PRELOAD=libvtkCommonCore-custom1.so set, no errors appear.
I would be grateful for any hint how to continue debugging this issue.
A minimum example of the problem is comprised by these files:
libvtkCommonCore-custom1.cpp:
#include <iostream>
void SymbolX()
{
std::cout<<"This just does nothing useful."<<std::endl;
}
libA.cpp:
void SymbolX(); // in libvtkCommonCore-custom1.so
struct LibAStaticObject
{
LibAStaticObject()
{
SymbolX();
}
} libAStaticObject;
libB.cpp:
#include <dlfcn.h>
#include <iostream>
class LibALoader
{
public:
LibALoader()
{
void *handle = dlopen ( "libA.so", RTLD_LAZY|RTLD_GLOBAL|RTLD_NODELETE );
if ( !handle )
{
std::cerr<<"Could not load module library libA!\nReason: " << dlerror() << std::endl;
}
}
} libAloader;
libC.cpp
/*empty*/
executable_situation1.cpp:
#include <iostream>
int main(int argc, char*argv[])
{
std::cout<<"starting."<<std::endl;
return 0;
}
executable_situation2.cpp
#include <iostream>
#include <dlfcn.h>
class LibCLoader
{
public:
LibCLoader()
{
void *handle = dlopen ( "libC.so", RTLD_LAZY|RTLD_GLOBAL|RTLD_NODELETE );
if ( !handle )
{
std::cerr<<"Could not load module library libC.so!\nReason: " << dlerror() << std::endl;
}
}
} libCloader;
int main(int argc, char*argv[])
{
std::cout<<"starting."<<std::endl;
return 0;
}
CMakeLists.txt:
add_library(vtkCommonCore-custom1 SHARED libvtkCommonCore-custom1.cpp)
add_library(A SHARED libA.cpp)
add_library(B SHARED libB.cpp)
target_link_libraries(B dl)
add_library(C SHARED libC.cpp)
target_link_libraries(C vtkCommonCore-custom1 B)
set_target_properties(C PROPERTIES LINK_FLAGS "-Wl,--add-needed -Wl,--no-as-needed -Wl,--copy-dt-needed-entries")
add_executable(executable_situation1 executable_situation1.cpp)
target_link_libraries(executable_situation1 vtkCommonCore-custom1 B)
set_target_properties(executable_situation1 PROPERTIES LINK_FLAGS "-Wl,--add-needed -Wl,--no-as-needed -Wl,--copy-dt-needed-entries") #"-Wl,--no-as-needed")
add_executable(executable_situation2 executable_situation2.cpp)
target_link_libraries(executable_situation2 dl)
Run it by these commands:
$ mkdir build
$ cd build
$ cmake .. && make
$ LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH ./executable_situation1
This just does nothing useful.
starting.
$ LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH ./executable_situation2
./executable_situation2: symbol lookup error: ./libA.so: undefined symbol: _Z7SymbolXv
Indeed, the problem is that in situation 2 the libvtkCommonCore is not in the lookup scope of libA while in situation 1 it is in the global scope.
The only (probably ugly) solution I found was to put in a kind of a stub library that loads libvtkCommonCore along with libB using "dlopen" with option "RTLD_GLOBAL". This places libvtkCommonCore in the global lookup scope. The new library is then linked to libC instead of its direct dependencies.

OpenGL EGL eglGetDisplay keeps return EGL error 0x3008(EGL_BAD_DISPLAY )

My ubuntu version is 16.04, and I first installed mesa-common-dev, libgl1-mesa-dev, libglm-dev, libegl1-mesa-dev. Then I installed NVIDIA-Linux-x86_64-440.64.run with opengl support.
But when I tried to run a toy example, I keep getting this error main: Assertion display != EGL_NO_DISPLAY failed
/* Compile with gcc -g3 -o example example.c -lX11 -lEGL */
#include <assert.h>
#include <stdio.h>
#include <EGL/egl.h>
#include <EGL/eglplatform.h>
void printEGLError();
int main(void) {
Display* x_display = XOpenDisplay(NULL);
EGLDisplay display = eglGetDisplay(x_display);
// EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
assert(display != EGL_NO_DISPLAY);
EGLint major, minor;
eglInitialize(display, &major, &minor);
char *string = eglQueryString(display, EGL_CLIENT_APIS);
assert(string);
printf("%s\n", string);
return 0;
}
/* Use printEGLError to show a description of the last EGL Error.
The descriptions are taken from the eglGetError manual */
#define ERROR_DESC(...) fprintf(stderr, "%s\n", __VA_ARGS__); break
void printEGLError() {
switch(eglGetError()) {
case(EGL_SUCCESS):
ERROR_DESC("The last function succeeded without error.");
case(EGL_NOT_INITIALIZED):
ERROR_DESC("EGL is not initialized, or could not be initialized, for the specified EGL display connection.");
case(EGL_BAD_ACCESS):
ERROR_DESC("EGL cannot access a requested resource (for example a context is bound in another thread).");
case(EGL_BAD_ALLOC):
ERROR_DESC("EGL failed to allocate resources for the requested operation.");
case(EGL_BAD_ATTRIBUTE):
ERROR_DESC("An unrecognized attribute or attribute value was passed in the attribute list.");
case(EGL_BAD_CONTEXT):
ERROR_DESC("An EGLContext argument does not name a valid EGL rendering context.");
case(EGL_BAD_CONFIG):
ERROR_DESC("An EGLConfig argument does not name a valid EGL frame buffer configuration.");
case(EGL_BAD_CURRENT_SURFACE):
ERROR_DESC("The current surface of the calling thread is a window, pixel buffer or pixmap that is no longer valid.");
case(EGL_BAD_DISPLAY):
ERROR_DESC("An EGLDisplay argument does not name a valid EGL display connection.");
case(EGL_BAD_SURFACE):
ERROR_DESC("An EGLSurface argument does not name a valid surface (window, pixel buffer or pixmap) configured for GL rendering.");
case(EGL_BAD_MATCH):
ERROR_DESC("Arguments are inconsistent (for example, a valid context requires buffers not supplied by a valid surface).");
case(EGL_BAD_PARAMETER):
ERROR_DESC("One or more argument values are invalid.");
case(EGL_BAD_NATIVE_PIXMAP):
ERROR_DESC("A NativePixmapType argument does not refer to a valid native pixmap.");
case(EGL_BAD_NATIVE_WINDOW):
ERROR_DESC("A NativeWindowType argument does not refer to a valid native window.");
case(EGL_CONTEXT_LOST):
ERROR_DESC("A power management event has occurred. The application must destroy all contexts and reinitialise OpenGL ES state and objects to continue rendering. ");
}
}
More Information: my graphics card is Titan Xp and I tried to run sudo servide lightdm stop and removed all possible remote desktop softwares. But the problem still exists. Anyone could help?
For those who may be confused about this problem, just unset DISPLAY. This may save your day.

How to save the contents of a COM object to a file Visual C++

I'm working my way through a DirectX11 tutorial and one of the "extra credit" exercises is to compile a shader and save the compiled code to a .shader file.
The program I ended up writing is super simple so I'll just list it here instead of explaining it in English.
The main program:
#include <windows.h>
#include <windowsx.h>
#include <d3d11.h>
#include <d3dx11.h>
#include <d3dx10.h>
#pragma comment (lib, "d3d11.lib")
#pragma comment (lib, "d3dx11.lib")
#pragma comment (lib, "d3dx10.lib")
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
ID3D10Blob *VS, *Errors;
D3DX11CompileFromFile(L"shaders.shader", 0, 0, "VShader", "vs_4_0", 0, 0, 0, &VS, &Errors, 0);
if(Errors)
MessageBox(NULL, L"The vertex shader failed to compile.", L"Error", MB_OK);
else if(!Errors)
MessageBox(NULL, L"The vertex shader compiled successfully.", L"Message", MB_OK);
HANDLE sHandle = CreateFile(L"compiledShader.shader",
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if(WriteFile(sHandle, VS->GetBufferPointer(), VS->GetBufferSize(), NULL, NULL)) // <--- break point
MessageBox(NULL, L"The compiled shader was saved successfully to 'compiledSahder.shader'", L"Message", MB_OK);
else
MessageBox(NULL, L"The vertex shader was not saved successfull.", L"Error", MB_OK);
return 0;
}
And the shader:
float4 VShader(float4 position : POSITION) : SV_POSITION
{
return position;
}
Since the contents of the VS Blob is where the compiled code is my first instinct is that I need to save the contents of that Blob to a .shader file. The only file writing I've worked with before is using fstream in basic C++ in linux so I'm unsure how to approach this. I've dug through the MSDN library for various file writing functions but I couldn't find anything that would let me write the content of a COM object to a file.
Let me know if you need any more information, and thanks in advanced for your help.
EDIT: the code has been updated per the suggestions of the current answer. I'm not 100% sure if I'm using these functions properly as I had to intuit how to by reading their descriptions on MSDN, but I think I'm on the right track. It creates the file, but when I try to write to it I'm getting the following error:
First-chance exception at 0x755cdeef in shader compiler.exe: 0xC0000005: Access violation writing location 0x00000000.
Unhandled exception at 0x755cdeef in shader compiler.exe: 0xC0000005: Access violation writing location 0x00000000.
The program '[5604] shader compiler.exe: Native' has exited with code -1073741819 (0xc0000005).
I feel like it's a simple mistake that is causing this problem. Any ideas?
EDIT #2: Here is what gets written to the file "compiledShader.shader"
Compiled shader code?
Does this look correct? If so I'll close the question as answered and worry about whatever is causing the generic access error on my own.
Look at the ID3D10Blob interface for the content, and check the CreateFile, WriteFile and CloseHandle Win32 functions.

visual studio 2008 error C2371: 'int8_t' : redefinition; different basic types (http_parser.h)

i try to compile simple c/c++ app that is using http_parser from node.js
i also using libuv , and basically trying to compile this example in windows.
using visual studio 2008
but i getting this compilation error :
>d:\dev\cpp\servers\libuv\libuv_http_server\http_parser.h(35) : error C2371: 'int8_t' : redefinition; different basic types
1> d:\dev\cpp\servers\libuv\libuv-master\libuv-master\include\uv-private\stdint-msvc2008.h(82) : see declaration of 'int8_t'
the code in the http_parser.h file looks like this:
#include <sys/types.h>
#if defined(_WIN32) && !defined(__MINGW32__) && (!defined(_MSC_VER) || _MSC_VER<1600)
#include <BaseTsd.h>
#include <stddef.h>
//#undef __int8
typedef __int8 int8_t;
typedef unsigned __int8 uint8_t;
typedef __int16 int16_t;
typedef unsigned __int16 uint16_t;
typedef __int32 int32_t;
typedef unsigned __int32 uint32_t;
typedef __int64 int64_t;
typedef unsigned __int64 uint64_t;
#else
#include <stdint.h>
#endif
as you can see i tryed to undef it but it didnt worked.
what can i do so it will pass compilation .
if i just remove it im getting this error :
http_parser.c(180) : error C2061: syntax error : identifier 'unhex'
on this code section :
static const int8_t unhex[256] =
{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1
,-1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1
,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
,-1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1
,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
};
and many other sections that using int8_t
As it's a typedef, you can't use #ifdef or #undef etc, because these only work with symbols that have been #define'ed.
The best you can do is to make sure that the two typedef's agree, the there should be no problem.
Looking at stdint-msvc2008.h, it might be easier to modify http_parser.h to this:
typedef signed __int8 int8_t;
Any good?
Try #define HAVE_STDINT_H 1 at the top of your code to avoid redefinition of int8_t.

error C2065: 'CComQIPtr' : undeclared identifier

I'm still feeling my way around C++, and am a complete ATL newbie, so I apologize if this is a basic question. I'm starting with an existing VC++ executable project that has functionality I'd like to expose as an ActiveX object (while sharing as much of the source as possible between the two projects).
I've approached this by adding an ATL project to the solution in question, and in that project have referenced all the .h and .cpp files from the executable project, added all the appropriate references, and defined all the preprocessor macros. So far so good. But I'm getting a compiler error in one file (HideDesktop.cpp). The relevant parts look like this:
#include "stdafx.h"
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <WinInet.h> // Shell object uses INTERNET_MAX_URL_LENGTH (go figure)
#if _MSC_VER < 1400
#define _WIN32_IE 0x0400
#endif
#include <atlbase.h> // ATL smart pointers
#include <shlguid.h> // shell GUIDs
#include <shlobj.h> // IActiveDesktop
#include "stdhdrs.h"
struct __declspec(uuid("F490EB00-1240-11D1-9888-006097DEACF9")) IActiveDesktop;
#define PACKVERSION(major,minor) MAKELONG(minor,major)
static HRESULT EnableActiveDesktop(bool enable)
{
CoInitialize(NULL);
HRESULT hr;
CComQIPtr<IActiveDesktop, &IID_IActiveDesktop> pIActiveDesktop; // <- Problematic line (throws errors 2065 and 2275)
hr = pIActiveDesktop.CoCreateInstance(CLSID_ActiveDesktop, NULL, CLSCTX_INPROC_SERVER);
if (!SUCCEEDED(hr))
{
return hr;
}
COMPONENTSOPT opt;
opt.dwSize = sizeof(opt);
opt.fActiveDesktop = opt.fEnableComponents = enable;
hr = pIActiveDesktop->SetDesktopItemOptions(&opt, 0);
if (!SUCCEEDED(hr))
{
CoUninitialize();
// pIActiveDesktop->Release();
return hr;
}
hr = pIActiveDesktop->ApplyChanges(AD_APPLY_REFRESH);
CoUninitialize();
// pIActiveDesktop->Release();
return hr;
}
This code is throwing the following compiler errors:
error C2065: 'CComQIPtr' : undeclared identifier
error C2275: 'IActiveDesktop' : illegal use of this type as an expression
error C2065: 'pIActiveDesktop' : undeclared identifier
The two weird bits: (1) CComQIPtr is defined in atlcomcli.h, which is included in atlbase.h, which is included in HideDesktop.cpp; and (2) this file is only throwing these errors when it's referenced in my new ATL/AX project: it's not throwing them in the original executable project, even though they have basically the same preprocessor definitions. (The ATL AX project, naturally enough, defines _ATL_DLL, but I can't see where that would make a difference.)
My current workaround is to use a normal "dumb" pointer, like so:
IActiveDesktop *pIActiveDesktop;
HRESULT hr = ::CoCreateInstance(CLSID_ActiveDesktop,
NULL, // no outer unknown
CLSCTX_INPROC_SERVER,
IID_IActiveDesktop,
(void**)&pIActiveDesktop);
And that works, provided I remember to release it. But I'd rather be using the ATL smart stuff.
Any thoughts?
You may have forgotten the namespace ATL
ATL::CComQIPtr

Resources