Hey guys,
I am trying to use a certain function from a VC++ DLL in my VB6 project. The original VC++ function looks like this:
When integrating the code in VB6, I wrote the following:Code:DLLEXPORT UNI_STATUS sysTabRead (const void *pTopicString, const int topicIndex, const void *pItemString, const int itemIndex, void *tableAddrs, int tableSize, SYS_TAB_ACCESS_TYPE accessType) typedef enum { /* bit field */ DYNAMIC=1, SAVED=2, DEFAULT=4, MINIMUM=8, MAXIMUM=16, } SYS_TAB_ACCESS_TYPE; typedef enum {UNI_ERROR=-1, UNI_OK=0} UNI_STATUS;
Now, here comes the tricky part, I have absolutely no idea where the "ByVal" declaration is necessary. I do know, however, that "ByRef" for tabelAddrs is required because tableAddrs is a pointer. The funny thing is that when I call the function, for example, in the following test subroutine:vb Code:
Public Enum SYS_TAB_ACCESS_TYPE DYNAMIC = &H1& SAVED = &H2& 'DEFAULT = &H4& MINIMUM = &H8& MAXIMUM = &H10& End Enum Public Enum UNI_STATUS UNI_ERROR = -1& UNI_OK = 0& End Enum Public Declare Function sysTabRead Lib "systab.dll" ( _ ByVal pTopicString As String, _ ByVal topicIndex As Long, _ ByVal pItemString As String, _ ByVal itemIndex As Long, _ ByRef tableAddrs As Any, _ ByVal tableSize As Long, _ ByVal accessType As SYS_TAB_ACCESS_TYPE) As UNI_STATUS
I receive a run-time error (49): "Bad DLL calling convention". However, if I choose to disregard the error (On Error Resume Next...), the function actually works, errRet returning as 0 (UNI_OK), and tableAddrs returning as the long value I was expecting. The thing is that a couple of seconds after the sub ends, VB6 crashes due to an incorrect memory "read" request...vb Code:
Public Sub Test() Dim errRet As UNI_STATUS, tableAddrs As Long errRet = sysTabRead("testTopic", 0, "testKey", 0, tableAddrs, 4, DYNAMIC) Debug.Print errRet Debug.Print tableAddrs End Sub
I'm pretty sure that my problem is with the ByVals or with the argument types... Any ideas?




Reply With Quote