Warning C4091: ' __declspec(dllexport)' - declspec

I have the following code where I am trying to export a function called "Interface_API" out of my dll.
#ifdef INTERFACEDLL_EXPORTS
#define UserApp_API __declspec(dllexport);
#else
#define UserApp_API __declspec(dllimport);
#endif
UserApp_API int Interface_API(int *, int *, int *);
When I compile this code it gives the following warning and the function is not getting exported.
warning C4091: ' __declspec(dllexport)' : ignored on left of 'int' when no variable is declared
When I change the declaration as given below I don't get the warning and it exports properly.
__declspec(dllexport) int Interface_API(int *, int *, int *);
I am little confused because I have used it in different dll and it works fine. Any clue?

#define UserApp_API __declspec(dllimport);
^ Semicolon.

Related

How to use NtOpenProcess

I am trying to use NtOpenProcess() I have not find any example in town.
I am getting an error any help is much appreciated.
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, PSTR szCmdLine, int showCmd)
{
HANDLE handle;
HWND myWindow =FindWindow(NULL, L"Notepad");
PCLIENT_ID PID;
GetWindowThreadProcessId(myWindow, (LPDWORD)&PID);
ZwOpenProcess(&handle, PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE, NULL,PID);
return 0;
}
The errors are
1>c:\users\asus\source\repos\windowsproject2\windowsproject2\windowsproject2.cpp(14): error C2065: 'PCLIENT_ID': undeclared identifier
1>c:\users\asus\source\repos\windowsproject2\windowsproject2\windowsproject2.cpp(14): error C2146: syntax error: missing ')' before identifier 'PID'
1>c:\users\asus\source\repos\windowsproject2\windowsproject2\windowsproject2.cpp(14): error C3861: 'NtOpenProcess': identifier not found
1>c:\users\asus\source\repos\windowsproject2\windowsproject2\windowsproject2.cpp(14): error C2146: syntax error: missing ';' before identifier 'PID'
1>c:\users\asus\source\repos\windowsproject2\windowsproject2\windowsproject2.cpp(14): error C2059: syntax error: ')'
This are my include files.
#include <Windows.h>
#include <ntddk.h>
#include <Ntifs.h>
#include "stdafx.h"
at first look at code:
FindWindow(NULL, L"Notepad");
faster of all you want
FindWindow(L"Notepad", 0);
because L"Notepad" is class name (not window name) and class name first parameter.
PCLIENT_ID PID;
GetWindowThreadProcessId(myWindow, (LPDWORD)&PID);
the GetWindowThreadProcessId wait pointer to DWORD memory, where it store process id. but you pass to it uninitialized pointer, to random memory. need use this:
CLIENT_ID pid = { };
if (GetWindowThreadProcessId(myWindow, (PDWORD)&pid.UniqueProcess))
finally ObjectAttributes in call NtOpenProcess is mandatory parameter and can not be 0.
about undeclared identifiers - all this declared in ntifs.h and it sub-headers (ntifs.h include ntddk.k - so you not need include it direct). problem that windows.h and ntifs.h is conflict - many common declarations. if you include both - you got a lot of errors. but solution exist - include ntifs.h in some namespace. but even after this you got some errors. but this also can be fixed, if deep understand source of errors. also you will be need include own code to this namespace too, for have easy access to ntifs declarations. and finally you need use ntdll.lib or ntdllp.lib (will be conflict with CRT libs if you use it) as linker input.
so if you want use native api in own code, without add custom headers, where you copy-paste some nt definitions and call it without resolve api in runtime, but use static linking - this is possible, but require deep knowledge and understanding what you doing. example
#define DECLSPEC_DEPRECATED_DDK
#define _XX_BEGIN namespace XX {
#define _XX_END }
_XX_BEGIN
struct _SECURITY_QUALITY_OF_SERVICE;
struct _CONTEXT;
_XX_END
#define _INC_MMSYSTEM /* Prevent inclusion of mmsystem.h in windows.h */
#include <windows.h>
#pragma warning(disable : 4005)
_XX_BEGIN
#ifdef _RTL_RUN_ONCE_DEF
#undef _RTL_RUN_ONCE_DEF
#endif
#define RtlCompareMemory ::RtlCompareMemory
#include <ntifs.h>
_XX_END
#undef _INC_MMSYSTEM /* Prevent inclusion of mmsystem.h in windows.h */
#include <MMSystem.h>
_XX_BEGIN
void demo()
{
if (HWND myWindow = FindWindow(L"Notepad", 0))
{
CLIENT_ID pid = { };
if (GetWindowThreadProcessId(myWindow, (PDWORD)&pid.UniqueProcess))
{
HANDLE handle;
static OBJECT_ATTRIBUTES zoa = { sizeof(zoa) };
if (0 <= NtOpenProcess(&handle,
PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE,
&zoa, &pid))
{
NtClose(handle);
}
}
}
}
_XX_END

#including <alsa/asoundlib.h> and <sys/time.h> results in multiple definition conflict

Here is the minimal C program to reproduce:
#include <alsa/asoundlib.h>
#include <sys/time.h>
int main( void )
{
}
This will compile with gcc -c -o timealsa.o timealsa.c, but if you include the -std=c99 switch, you get a redefinition error:
In file included from /usr/include/sys/time.h:28:0,
from timealsa.c:3:
/usr/include/bits/time.h:30:8: error: redefinition of ‘struct timeval’
struct timeval
^
In file included from /usr/include/alsa/asoundlib.h:49:0,
from timealsa.c:2:
/usr/include/alsa/global.h:138:8: note: originally defined here
struct timeval {
^
How can I resolve this conflict while still using -std=c99?
Since your question suggests you are using GLIBC's time.h there is a way to avoid this by telling it not to define timeval. Include asoundlib.h first then define _STRUCT_TIMEVAL. The one defined in asoundlib.h will be the one that gets used.
#include <alsa/asoundlib.h>
#ifndef _STRUCT_TIMEVAL
# define _STRUCT_TIMEVAL
#endif
#include <sys/time.h>
int main( void )
{
}
With C99 and later you can't have duplicate definitions of the same struct. The problem is that alsa/asoundlib.h includes alsa/global.h which contains this code:
/* for timeval and timespec */
#include <time.h>
...
#ifdef __GLIBC__
#if !defined(_POSIX_C_SOURCE) && !defined(_POSIX_SOURCE)
struct timeval {
time_t tv_sec; /* seconds */
long tv_usec; /* microseconds */
};
struct timespec {
time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};
#endif
#endif
So the Michael Petch's solution won't work - by the time you've included alsa/asoundlib.h it is already too late. The proper solution is to define _POSIX_C_SOURCE (_POSIX_SOURCE is obsolete). There's more information about these macros here and here.
For example you could try -D_POSIX_C_SOURCE=200809L. However, if you do that you'll get errors like this:
/usr/include/arm-linux-gnueabihf/sys/time.h:110:20: error: field ‘it_interval’ has incomplete type
struct timeval it_interval;
^
/usr/include/arm-linux-gnueabihf/sys/time.h:112:20: error: field ‘it_value’ has incomplete type
struct timeval it_value;
^
/usr/include/arm-linux-gnueabihf/sys/time.h:138:61: error: array type has incomplete element type
extern int utimes (const char *__file, const struct timeval __tvp[2])
^
This is all a big mess of old C code and macro madness. The only way I got it to work was to give up and use -std=gnu11.

Visual Studio Syntax Error on offsetof()

I have type:
typedef struct
{
int x;
int y;
int z;
} sdf_test_t;
But when I try to compile the following:
offset = offsetof(sdf_test_t, z);
Visual Studio responds with:
c:\dataflash.c(542) : error C2143: syntax error : missing ')' before 'type'
c:\dataflash.c(542) : error C2059: syntax error : ')'
What is wrong here?
I am using:
Microsoft Visual Studio 2008 x86
Microsoft (R) Visual Studio Version 9.0.21022.8.
The offsetof macro is defined in <stddef.h> as follows:
/* Define offsetof macro */
#ifdef __cplusplus
#ifdef _WIN64
#define offsetof(s,m) (size_t)( (ptrdiff_t)&reinterpret_cast<const volatile char&>((((s *)0)->m)) )
#else
#define offsetof(s,m) (size_t)&reinterpret_cast<const volatile char&>((((s *)0)->m))
#endif
#else
#ifdef _WIN64
#define offsetof(s,m) (size_t)( (ptrdiff_t)&(((s *)0)->m) )
#else
#define offsetof(s,m) (size_t)&(((s *)0)->m)
#endif
#endif /* __cplusplus */
By elimination. I've established that the compiler uses:
#define offsetof(s,m) (size_t)&reinterpret_cast<const volatile char&>((((s *)0)->m))
I've made a simple program as it:
#include <stddef.h>
typedef struct
{
int x;
int y;
int z;
} sdf_test_t;
int main() {
size_t offset = offsetof(sdf_test_t, z);
return 0;
}
I don't have any problems, i think that you can try to isolate the code in another project and test it again.
I managed to fix it by adding the following line to my source file:
#include <stddef.h>
From this, it seems that Visual Studio silently includes header files if you don't include them explicitly. Even worse, It assumes that the source file is C++ by default.
If I don't include a header file with a symbol I use, I would expect the compiler to scream out and report an error, not just make up something...

chdir not declared, compilation error g++

I am trying to compile a relatively simple application that I obtained from the web..
When running make I get the following error:
In file included from main.cpp:2:0:
os.h: In function ‘void myOpenDir(const char*)’:
os.h:13:16: error: ‘chdir’ was not declared in this scope
The file os.h looks like this:
#ifndef OS_H
#define OS_H
#if defined(__GNUG__)
#define INT64 long long
#define UINT64 unsigned long long
#include <dirent.h>
#define SPRTR '/'
void myOpenDir(const char* dirpath)
{
chdir(dirpath);
}
#elif defined(_MSC_VER)
#define INT64 __int64
#define UINT64 unsigned __int64
#include <direct.h>
#define SPRTR '\\'
void myOpenDir(const char* dirpath)
{
_chdir(dirpath);
}
#else
#error "Platform not supported. Need to update source code"
#endif
#endif
Someone got an idea why it wont compile?
I also used a g++ compiler via g++-4.7.real -c main.cpp but so far no luck.
Add #include <unistd.h>, as per the chdir manual.

Call C++ library from main.mm results in compile error - verify declaration conflicts with macro

I have the following code in a header included in main.mm:
1. virtual int truncate(DbTxn *, u_int32_t *, u_int32_t);
2. virtual int upgrade(const char *name, u_int32_t flags);
3. virtual int verify(
4. const char *, const char *, __DB_STD(ostream) *, u_int32_t);
The first two lines are for context and to show what is working. The third and fourth lines have the following errors:
Macro "verify" passed 4 arguments, but takes just 1
'verify' declared as a 'virtual' field
If I add a random character to the end of the verify declaration like verityx then the file compiles without a problem. Is verify reserved?
Edit:
My main.mm file:
#import <Foundation/Foundation.h>
#import "db_cxx.h"
int main (int argc, const char * argv[])
{
return 0;
}
Edit 2:
The only two uses of the word "verify" in the berkeley header are:
virtual int log_verify(DB_LOG_VERIFY_CONFIG *);
virtual int verify(
const char *, const char *, __DB_STD(ostream) *, u_int32_t);
Macro "verify" passed 4 arguments, but takes just 1
means that there's a #define verify(x) ... somewhere. It's not reserved in C++ but something you're including is defining it.
A quick
fgrep -r verify /usr/include | fgrep '#define'
yields, amongst a lot of other things,
/usr/include/AssertMacros.h: #define verify(assertion) __Verify(assertion)
After you've included all the OS X/iOS headers you need, it should be safe to #undef verify before including bdb.

Resources