开发者

Extern points to 0x00000000

开发者 https://www.devze.com 2023-02-03 19:26 出处:网络
I am using Microsoft Visual C++ 2010 Express, on Windows XP. I have one application that produces a DLL, a header file in this application also declares a pointer to a BUFFER as an extern.

I am using Microsoft Visual C++ 2010 Express, on Windows XP.

I have one application that produces a DLL, a header file in this application also declares a pointer to a BUFFER as an extern. To register this DLL with the system, I drag it onto the regsvr32.exe icon in the system32 folder.

I have another application that tests the use of this DLL, by initialising and calling functions from it. This application accesses the same BUFFER as the DLL, by using this extern declaration.

At first, when I used the Visual Studio debugger to debug the test application, I could see the contents of the extern BUFFER from the loaded DLL. However, after repeatedly debugging this application, now the BUFFER does not display its memory address, just "0x0000000", so I can't view the data开发者_高级运维.

Does anyone know why this might be? I can't understand why it used to work, but now doesn't. I haven't changed any aspect of this part of the source code, at all. Is it OK to access the BUFFER in the DLL by using an extern pointer like this, or is there a better way?

Thanks for your help.


In C++ extern means, that variable is declared in another *.cpp (translation unit). Example: myfile1.cpp:

int globalVariable = 0;

myfile2.cpp:

extern int globalVariable; //same variable, because of extern

If you need to export from dll, you must use dllexport (in library) and dllimport (in library consumer) for functions and variables, e.g.: mylibrary.cpp:

__declspec(dllexport) int myGlobalExportingVariable = 0;

myprogram.cpp:

__declspec(dllimport) int myGlobalExportingVariable;

Of course, in real world, you would probably use something like this: mylibrary.hpp:

#ifdef MYLIBRARY
#define MYLIBRARY_ITEM __declspec(dllexport)
#else
#define MYLIBRARY_ITEM __declspec(dllimport)
#endif

MYLIBRARY_ITEM void func1();
MYLIBRARY_ITEM int variable0;
MYLIBRARY_ITEM float func2();
//...

And you #include this header in both mylibrary.cpp and myprogram.cpp; don't forget to define MYLIBRARY macro in your project settings (C++ -> Preprocessor -> Preprocessor definitions).

By the way: as pointed in comment by PiotrLegnica, registering your dll library with regsvr32.exe is pointless unless you use technology COM (component object model).


When you declare a variable with extern then it’s mean only declare it but not define it( no memory allocation for that variable) because its define it other place. You declare the variable in your cpp file with dll codes. In header make it as extern with dllexport/import and now use it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号