Situation:
- I'm making a C library and I'm calling its functions from VB6
- One of the C functions is acting really weird when one line is uncommented
Here's the C prototype of that function:
BOOL __stdcall GetNVTemp ( INT dwDispNum, PINT lpdwCoreTemp, PINT lpdwAmbientTemp, PINT lpdwThreshold, PUCHAR errMsg );
And the VB declaration:
Private Declare Function GetNVTemp Lib "xthermintr.dll" (ByVal dwDispNum As Long, ByRef lpdwCoreTemp As Long, ByRef lpdwAmbientTemp As Long, ByRef lpdwThreshold As Long, ByRef errMsg As Byte) As Integer
And the VB call:
Const ERR_SIZE = 2048
Private errMsg() as Byte ' global
Dim coreTemp As Long, ambTemp As Long, threshold As Long
ReDim errMsg(0 To (ERR_SIZE - 1)) As Byte
errMsg = StrConv("asdf this is definitely longer than 40 characters blah blah blah asddasd dd blah blah blah" & Chr(0), vbFromUnicode)
...If (CTRUE = GetNVTemp(0, coreTemp, ambTemp, threshold, errMsg(0))) Then...
--------------------------
errMsg() is just a sequence of bytes containing an ASCII string. Before GetNVTemp is called, it is filled with some string. Within the C function I have it overwrite the first 40 characters with *s. The function works and the string comes back as expected with the first 40 characters overwritten. If I increase the memset to set one more character (41) to a '*', I get an error message in VB stating 'Bad DLL calling convention' (probably indicating corrupted memory). What's up with that?
Here's the twist: If I comment out the line labeled "/*ERRONEOUS LINE*/", I can set the memset length value to whatever I want (even to ERR_SIZE(2048)) and I won't get any errors in VB. Just by commenting out that one line everything is fixed. What can I assume from that? I am passing the NvGet.. function the correct size of variables (I even tried making them unsigned 64-bit ints vs. the current DWORDs, and it still happened). The memset should be good up to 2048 (ERR_SIZE, the size of the errMsg variable in bytes), but it is only good up until 40 for an unknown reason. Beyond that, instant 'bad convention' error. Yet, the data is intact, and the pointer to character 41 is valid, at least before the C function is called.
I had another function (see GetNVTemp2 of attached code) where I had it clear out errMsg in case there was an error. And, I also had it alert me with a MessageBox before hand (yes I am sure the MessageBox works as it does elsewhere). Keep in mind that there are no errors throughout this entire function, and I saw no MessageBoxes indicating an error had occurred (plus it makes no sense that there would have been an error). The MessageBox and memset lines should only be called under the condition that there was an error (if hNvCplLib == NULL). Alright, now when the "/*ERRONEOUS LINE 2*/" even exists under the false IF statement, I get the bad DLL calling convention error again. The line isn't even getting called, and it's causing a problem. What sense does that make?? As soon as I comment out that memset line under the IF statement (which shouldn't even be getting called), I get no more errors. I am totally puzzled. Any guidance is appreciated here.
P.S. PINT is INT*, PUCHAR is UCHAR*. I am quite sure that the way I am sending the variables is not a problem. I have used several functions without issue, and they can write to the errMsg variable just fine. I haven't had a problem until I tried NVIDIA's API which seems to be corrupting my code somehow. There is no data type I am passing to this GetNVTemp function that I have not passed to other functions before.
- I'm making a C library and I'm calling its functions from VB6
- One of the C functions is acting really weird when one line is uncommented
Here's the C prototype of that function:
BOOL __stdcall GetNVTemp ( INT dwDispNum, PINT lpdwCoreTemp, PINT lpdwAmbientTemp, PINT lpdwThreshold, PUCHAR errMsg );
And the VB declaration:
Private Declare Function GetNVTemp Lib "xthermintr.dll" (ByVal dwDispNum As Long, ByRef lpdwCoreTemp As Long, ByRef lpdwAmbientTemp As Long, ByRef lpdwThreshold As Long, ByRef errMsg As Byte) As Integer
And the VB call:
Const ERR_SIZE = 2048
Private errMsg() as Byte ' global
Dim coreTemp As Long, ambTemp As Long, threshold As Long
ReDim errMsg(0 To (ERR_SIZE - 1)) As Byte
errMsg = StrConv("asdf this is definitely longer than 40 characters blah blah blah asddasd dd blah blah blah" & Chr(0), vbFromUnicode)
...If (CTRUE = GetNVTemp(0, coreTemp, ambTemp, threshold, errMsg(0))) Then...
--------------------------
errMsg() is just a sequence of bytes containing an ASCII string. Before GetNVTemp is called, it is filled with some string. Within the C function I have it overwrite the first 40 characters with *s. The function works and the string comes back as expected with the first 40 characters overwritten. If I increase the memset to set one more character (41) to a '*', I get an error message in VB stating 'Bad DLL calling convention' (probably indicating corrupted memory). What's up with that?
Here's the twist: If I comment out the line labeled "/*ERRONEOUS LINE*/", I can set the memset length value to whatever I want (even to ERR_SIZE(2048)) and I won't get any errors in VB. Just by commenting out that one line everything is fixed. What can I assume from that? I am passing the NvGet.. function the correct size of variables (I even tried making them unsigned 64-bit ints vs. the current DWORDs, and it still happened). The memset should be good up to 2048 (ERR_SIZE, the size of the errMsg variable in bytes), but it is only good up until 40 for an unknown reason. Beyond that, instant 'bad convention' error. Yet, the data is intact, and the pointer to character 41 is valid, at least before the C function is called.
I had another function (see GetNVTemp2 of attached code) where I had it clear out errMsg in case there was an error. And, I also had it alert me with a MessageBox before hand (yes I am sure the MessageBox works as it does elsewhere). Keep in mind that there are no errors throughout this entire function, and I saw no MessageBoxes indicating an error had occurred (plus it makes no sense that there would have been an error). The MessageBox and memset lines should only be called under the condition that there was an error (if hNvCplLib == NULL). Alright, now when the "/*ERRONEOUS LINE 2*/" even exists under the false IF statement, I get the bad DLL calling convention error again. The line isn't even getting called, and it's causing a problem. What sense does that make?? As soon as I comment out that memset line under the IF statement (which shouldn't even be getting called), I get no more errors. I am totally puzzled. Any guidance is appreciated here.
P.S. PINT is INT*, PUCHAR is UCHAR*. I am quite sure that the way I am sending the variables is not a problem. I have used several functions without issue, and they can write to the errMsg variable just fine. I haven't had a problem until I tried NVIDIA's API which seems to be corrupting my code somehow. There is no data type I am passing to this GetNVTemp function that I have not passed to other functions before.