views:

49

answers:

1

I have a program that calls a function in a dynamically-linked library. All of the function calls work fine, except one.

double *frameData = NULL;
int frameDataSize = 0;
g_BeamGage_GetSingleFrame(BEAMGAGE_ID, &frameData, &frameDataSize); //error here

When BeamGage_GetSingleFrame is called when debugging my program, I get a runtime error: (Line 197, col 17 is the position of this function call)

FATAL RUN-TIME ERROR: "BeamGage Interface Test.c", line 197, col 17, thread id 0x00001724: The program has caused a 'Unknown' fault at 001B:7C812AFB.

I encountered this error before, when I was trying to use a NULL function pointer, but when using GetProcAddress, I check to make sure that the pointer is not NULL.

#define BEAMGAGE_FUNCTION_GETSINGLEFRAME            "BeamGage_GetSingleFrame"
typedef int(*dataFunc)(const char*, double**, int*);
dataFunc g_BeamGage_GetSingleFrame = NULL;

in main:
    ... //Load DLL, get other function pointers
    g_BeamGage_GetSingleFrame = (dataFunc)GetProcAddress(hBGDll, BEAMGAGE_FUNCTION_GETSINGLEFRAME);
    if(g_BeamGage_GetSingleFrame == NULL) {
        ... //error handling, program exits
    }
    ... //Get more function pointers

The function is defined in a C++/CLI project:

#define BEAMGAGE_IF __declspec(dllexport) _cdecl
extern "C" {
    int BEAMGAGE_IF BeamGage_GetSingleFrame(const char *id, double **dataArray, int *dataSize);
}

I am thoroughly stumped by this one, as the error code is rather unhelpful and the function call causing the error is very similar to the other function calls that cause no errors. The only difference I can think of is that this function takes non-const pointer arguments.

EDIT: Also, the code in the BeamGage_GetSingleFrame function (in the C++/CLI assembly) does not execute when the program tries to call it.

A: 

Try marking your function type explcitly __cdecl in case your build is defaulting to __stdcall:

typedef int __cdecl (*dataFunc)(const char*, double**, int*);

Some other shots in the dark:

I'm not sure what calling into a C++/CLI build DLL might require - as far as I know it's supposed to 'just work'. It seems to work for your other APIs, but maybe some research there might pay off.

Is it possible that GetSingleFrame() expects you to pass in a buffer for the dataArray?

Michael Burr
I just added the __cdecl, but it didn't change anything. I use the same method in the same C file to call other functions in the same C++/CLI DLL, and they all work fine.I coded GetSigleFrame myself, it allocates the memory for the buffer. Also, the code in the function never actually executes.
Eric Finn
So `GetSingleFrame()` is your own code in a C++/CLI compiled DLL? The calling code is native C/C++? And the call never reaches your code (ie., it's crashing in the marshaling code)? I'm not experienced with C++/CLI, but maybe the 2nd parameter needs to marked as output-only on the CLI -side of things (however that might be) so the marshaling code won't expect it to contain a non-NULL pointer. As a quick test, you might try initializing `frameData` to point to some small array of `double` even though you expect nothing to be done with it - just see if it prevents the crash.
Michael Burr
Yes, it's my own code, and the native code calling it is written in C. And yes, it crashes before reaching the C++/CLI code. And I'm not sending a NULL pointer to it - I'm sending a valid pointer to a NULL pointer, so it should have no problem if it checks for NULL pointers. Even so, I tried making a small array before passing the pointer in, but it still crashes.
Eric Finn
Sorry, it turns out that it's crashing inside the function itself, and my System::Console::WriteLine calls aren't printing to the console, which made me think that it actually wasn't executing.
Eric Finn
@Eric - the good thing about that is you should be able to easily see exactly what's going wrong with a breakpoint on the function. Sorry that my suggestions didn't get you anywhere; good luck with the debugging.
Michael Burr