I've got an error handler library that I use in all my projects. I've extended it to include a pathname it can use to output errors to the caller's preferred log file name. This works fine when the library source is attached to a project that needs it.
However, when I move to a DLL project which uses the same library, I'm running into problems.
I've simply done some DLLEXPORT / DLLIMPORT to expose this pathname to the DLL for its internal use. But when I do, I get errors when trying to strcpy or sprintf to it.
ErrorHandler.c
char errLog[MAX_PATHNAME_LEN] = {ERROR_LOG_FILE};
ErrorHandler.h
#define ERROR_LOG_FILE "error.log" // default output log file
extern char errLog[MAX_PATHNAME_LEN];
void ErrorMessage(char* logFile, double code, err_type_enum type, int line, char* function, char* file, int flags, char* string);
DLL source file.c
char DLLEXPORT errLog[MAX_PATHNAME_LEN];
DLL source file.h
char DLLIMPORT errLog[260];
The above is compiled into a DLL. Now at the test project which has this DLL attached, here are my tests...
ErrorMessage("C:\\Users\\user\\Desktop\\ERROR_NEW.log", 666, 1, 1, __FUNCTION__, __FILE__, ERR_OUT_LOG, "Oops!");
Passing this literal works!
sprintf(errLog, "C:\\Users\\user\\Desktop\\ERROR_2.log");
This gives a GPF, "FATAL RUN-TIME ERROR: "test.c", line 375, col 38, thread id 0x00002528: Out-of-bounds pointer argument (before start of memory block).
I get the same results for strcpy.
Now, check out my debugger... the errLog array can't be viewed in a Watch window:
Yet in the Variables window, you can clearly see both the correct size of the array (260) and the initialized contents...
What's going on here?