Compiler error:C3861 'GetModuleHandleEx': identifier not found - winapi

#include <windows.h>
#include <stdlib.h>
#include <tchar.h>
#ifdef __cplusplus
extern "C"
#endif
void * _ReturnAddress(void);
#pragma intrinsic(_ReturnAddress)
//I inserted the following code inside one of the functions
void func()
{
------------
-------
----
-
HMODULE module_handle;
TCHAR module_name[4096];
DWORD flag = 0x00000004;
GetModuleHandleEx(flag, (LPCTSTR) _ReturnAddress(), &module_handle);
GetModuleFileName(module_handle,module_name,4096);
-----
--
}
When I compile the code as a separate project everything works fine. Please help.

To compile an application that uses
this function, define _WIN32_WINNT as
0x0501 or later. For more information,
see Using the Windows Headers.

Even if you get your code to compile, what you're doing smells of wrongness.
If you're using the return address to make any sort of security-related decision, stop. You can't trust the return address of the calling function. No really, you can't trust the return address of the calling function.

Related

Why including cpp makes different result

l learned "include" keyword are just copy & paste.
But including cpp file makes different compile result.
(gcc6~8 + boost1.69)
// main.cpp
#include <iostream>
// I'll move next code to why.cpp
#include <boost/archive/iterators/base64_from_binary.hpp>
#include <boost/archive/iterators/binary_from_base64.hpp>
#include <boost/archive/iterators/transform_width.hpp>
#include <boost/archive/iterators/insert_linebreaks.hpp>
#include <boost/archive/iterators/remove_whitespace.hpp>
void testFunc()
{
using namespace boost::archive::iterators;
typedef transform_width<binary_from_base64<remove_whitespace<std::string::const_iterator>>, 8, 6> ItBinaryT;
std::string input;
std::string output(ItBinaryT(input.begin()), ItBinaryT(input.end()));
}
// -----------------------------
int main()
{
return 0;
}
Above code compiled without warning.
But, I replace some code with include cpp..
// main.cpp
#include <iostream>
#include "why.cpp" // <----------
int main()
{
return 0;
}
// why.cpp - just copy&paste
#include <boost/archive/iterators/base64_from_binary.hpp>
#include <boost/archive/iterators/binary_from_base64.hpp>
#include <boost/archive/iterators/transform_width.hpp>
#include <boost/archive/iterators/insert_linebreaks.hpp>
#include <boost/archive/iterators/remove_whitespace.hpp>
void testFunc()
{
using namespace boost::archive::iterators;
typedef transform_width<binary_from_base64<remove_whitespace<std::string::const_iterator>>, 8, 6> ItBinaryT;
std::string input;
std::string output(ItBinaryT(input.begin()), ItBinaryT(input.end()));
}
It makes warning [-Wsubobject-linkage]
~~ has a field ~~ whose type uses the anonymous namespace
~~ has a base ~~ whose type uses the anonymous namespace
Please look at this link : https://wandbox.org/permlink/bw53IK2ZZP5UWMGk
What makes this difference?
Your compiler treats the main CPP file specially under the assumption that things defined in it are very unlikely to have more than one definition and so some tests for possible violation of the One Definition Rule are not done inside that file. Using #include takes you outside that file.
I would suggest just not using -Wsubobject-linkage since its logic is based on a heuristic that is not applicable to your code.

How to setup C/C++ project safely (file organisation)

I have a (large) C/C++ project that consists of both C and C++ languages. At some point it turned out that there are two C functions with identical names. Those functions are defined in two different *.c files in different locations. In general at the highest level, the project is C++. This problem was questioned and answered here
However still a question "how to organize those files safely" remains. How can I group such project so that there are no name conflicts, and I can be sure that proper function is called. Will writing a wrapper for each of those functions help?
That how it looks at the moment:
A.h //first declaration of function F
A.c //first definition of function F
B.h //second declaration of function F
B.c //second definition of function F
trying to make such thing:
extern "C"{
#include "A.h"
#include "B.h"
}
causes of course name conflict. What can I do to avoid this conflct, and have the robust code? Would such solution help:
A_Wrapper.h: //c++
extern "C"{
#include "A.h"
}
void WrapF_A(int x)
{
F(x);
}
B_Wrapper.h: //C++
extern "C"{
#include "B.h"
}
void WrapF_B(int x)
{
F(x);
}
and then in the program:
#include A_Wrapper.h
#include B_Wrapper.h
Modyfing each file in that project would be rather impossible as it cosists of hundreds of files, and i would probably damage some code rather. Is there a way to make an include file seen only in some part of the program?
EDIT:
So I created a simple project illustrating the problem, and tried to apply the hints given by doctorlove. However still multiple definition of F error occurs. What should I change? Project files:
A.h:
#ifndef A_H_INCLUDED
#define A_H_INCLUDED
int F(int x);
#endif // A_H_INCLUDED
A.c
#include "A.h"
int F(int x)
{
return x*x;
}
AWrapper.h:
#ifndef AWRAPPER_H_INCLUDED
#define AWRAPPER_H_INCLUDED
int AF(int x);
#endif // AWRAPPER_H_INCLUDED
AW.cpp:
#include "AWrapper.h"
extern "C"{
#include "A.h"
}
int AF(int x)
{
return F(x);
}
B.h:
#ifndef B_H_INCLUDED
#define B_H_INCLUDED
int F(int x);
#endif // B_H_INCLUDED
B.c:
#include "B.h"
int F(int x)
{
return -x*x;
}
BWrapper.h:
#ifndef BWRAPPER_H_INCLUDED
#define BWRAPPER_H_INCLUDED
int BF(int x);
#endif // BWRAPPER_H_INCLUDED
BW.cpp:
#include "BWrapper.h"
extern "C"{
#include "B.h"
}
int BF(int x)
{
return F(x);
}
Go with your wrapper idea, but write a facade (see also here) that exposes what you need from A, and what you need from B not all the functions in there.
You will end up with something like
//header Wrap_A.h
#ifndef WRAP_A_INCLUDED
#define WRAP_A_INCLUDED
//for some input Data left as an exercise for the reader...
double solve_with_A(Data data);
#endif
//header Wrap_B.h
#ifndef WRAP_B_INCLUDED
#define WRAP_B_INCLUDED
//for some input Data...
double solve_with_B(Data data);
#endif
Then make two cpp files that include all the conflicting headers files, those from A in A.cpp and those from B in B.cpp, so the conflicts don't happen. The solve_with_A and solve_with_B functions will then call all the things they need without without leaking them to the whole program and causing conflicts.
You might have to give some thought to what Data will actually be. You could define your own types, one for A and one for B. Just avoid exposing the implementation details in your wrapping/facade headers.
If headers are causing you pain, firewall them off in the naughty corner.
EDIT
Given you have two functions, F, if you put all the sources into one project the linker should and will complain it can see both. Instead, you need to make two static libraries, and just expose the wrapped version to your main project.

C2487 with boost members

I try to reorganize our project from static libs into shared libraries of the subprojects.
Well, using VS Compiler all exporting classes needs a _declspec(dllexport) and importing them needs _declspec(dllimport). Works fine. But I got troubles with all classes derived from boost members (e.g. singleton, or ptr_map).
I get the error
error C2487: 'boost::serialization::singleton::instance' :
member of dll interface class may not be declared with dll interface
Microsofts solution is not very helpful, because changing boosts code would maybe not be a good idea;)
Is it not a good idea to export boost derived classes? Does anybody know where this comes from or maybe knows howto fix?
(samplecode below)
thanks!
Here's a sample (mylib.h as shared library project named: "myLib"):
#ifndef _MY_LIB_H_
#define _MY_LIB_H_
#include <string>
#include <boost/serialization/singleton.hpp>
using boost::serialization::singleton;
#ifdef MYLIB_EXPORTS
#define PORT_DLL __declspec(dllexport)
#else
#define PORT_DLL __declspec(dllimport)
#endif
class PORT_DLL MyLib
: singleton<MyLib>
{
public:
std::string GiveMeOutput() const;
};
#endif //_MY_LIB_H_
it's implementation (myLib.cpp)
#include "myLib.h"
std::string
MyLib::GiveMeOutput() const
{
return "some output";
}
an easy main.cpp (as executable project)
#include <iostream>
#include "../myLib/myLib.h"
int main()
{
MyLib lib;
std::cout << lib.GiveMeOutput();
return 0;
}
some points:
VS2010
x64
boost 1.52

bison parser compilation unkonwn errors

I am building a parser but I have some errors that I could not solve them, I am nuw to bison and flex, please help me solve them and understand why they are happening here is my errors that I get:
lexical.l:3:20: error: common.h: No such file or directory
In file included from lexical.l:5:
bison.tab.h:81: error: expected â=â, â,â, â;â, âasmâ or â__attribute__â before âyylvalâ
bison.tab.c:1155: error: conflicting types for âyylvalâ
bison.tab.h:81: note: previous declaration of âyylvalâ was here
bison.y: In function âyyparseâ:
bison.y:96: error: incompatible types when assigning to type âSTYPEâ from type âNODEPTRâ
here is my parser file bison.y:
%{
#include <stdio.h>
#include "bison.tab.h"
#include "common.h"
//int yylex();
void yyerror (char const *);
typedef struct STYPE {
NODEPTR pointer;
} STYPE;
#define YYSTYPE STYPE
%}
/* Bison declarations. */
%token ELSE REAL INTEGER XWRITE WHILE END DO IF THEN XPROGRAM FUNCTION XRETURN XREAD VAR FOR XBEGIN CALL ID NUM
%token RELOP ADDOP MULOP ASSIGN AND OR NOT
%left '-' '+'
%left '*' '/'
%nonassoc LOWER_THAN_ELSE
%nonassoc ELSE
If you #define YYSTYPE in your bison file, you need to #define YYSTYPE also in your flex file, because bison doesn't put the #define into the generated header file. You need to do this before you #include the generated header file.
Bison doesn't put the #define in the generated header because it has no way of knowing whether you did, since you might do it in an included file. In fact, if you're going to #define YYSTYPE, you should do it in a common header file, and #include the common header file in both bison and flex programs (as above, before you include the bison-generated header file).
Also, when you're regenerating the generated code, remember to always generate the bison program first because the flex program depends on the generated header file. That's the opposite order to the way you are doing it.
Just to make all this a bit clearer, here's an example:
common.h:
struct MyType {
/* ... /
};
#define YYSTYPE struct MyType;
lexer.l:
%{
/* All your standard includes go here */
/* Must go in this order */
#include "common.h"
#include "bison.tab.h"
%}
bison.y:
%{
/* Whatever library includes you need */
#include "common.h"
/* Don't include bison.tab.h; it will get inserted automatically */
%}
To fix the yytext error add this to bison.y :-
extern char *yytext
To fix the yyerror error make your prototype at the top of bison.y match the defn below :-
int yyerror(const char *message);
Fixing the yylval error requires a little more work and I don't understand this well enough to help. I'd suggest trying a simple hello,world type lexer,parser and move forward from there.
Here is the common.h I used :-
typedef struct STYPE {
int pointer;
} STYPE;
And the header of lexer :-
%{
#include "common.h"
#define YYSTYPE STYPE
#include <stdio.h>
#include"bison.tab.h"
void showToken(char*);
%}
And the header of parser :-
%{
#include <stdio.h>
#include "common.h"
extern char *yytext;
#define YYSTYPE STYPE
%}
This gives me one error and a few warnings but those are due to the undefined functions. Note that I have moved the declaration of STYPE to the top of lexer and parser

Trying to use SHCreateShellItem, having #include issues

There is a function in called SHCreateShellItem which is declared in <shlobj.h>, but it has been #ifdef'd out based on whether or not _WIN32_IE is greater than or equal to 0x601 (if it is, then the declaration is present). However, even when I define _WIN32_IE to 0x601 before I include <shlobj.h>, MSVC++ still complains that SHCreateShellItem is undeclared.
For example, I cannot get the following to compile:
#define _WIN32_IE 0x601
#include <shlobj.h>
int someFunction (LPITEMIDLIST parent, LPITEMIDLIST child)
{
HRESULT result;
IShellItem *shellObj;
result = SHCreateShellItem (parent, NULL, child, &shellObj);
if (SUCCEEDED(result))
{
// do stuff
}
return SUCCEEDED(result);
}
Do I need to define _WIN32_IE in a different way?
_WIN32_IE is usually defined in your stdafx.h file. You must change it there.
Don't define _WIN32_IE directly, do it implicitly with _WIN32_WINNT (which sets the target platform for the SDK includes)
#define _WIN32_WINNT 0x0600
#include <windows.h>
#include <shlobj.h>

Resources