C++ DLL in VB error on Returning a string

tnorton

Member
Joined
Sep 28, 2007
Messages
19
Programming Experience
1-3
I have had major troubles with this one. First of all I needed to work out how to convert char[] to string

Now my string returns the following error when used in VB
PInvokeStackImbalance was detected
Message: A call to PInvoke function 'IVcom!IVcom.Nortronics.Indigo.IVcomObjects::GetMAC' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.



VB.NET:
Public Declare Function GetMAC Lib "IVserial.dll" (ByVal IPddress As String) As String
     Public Function returnMAC(ByVal mIPaddress As String) As String
            Dim strMAC As String
            strMAC = GetMAC(mIPaddress)
            returnMAC = strMAC
        End Function

The C++ code
VB.NET:
string __stdcall GetMAC(const char *IPAddress) 
 {
    VBDEVICEINFO    deviceInfo;
    char           *pszIpAddr = NULL;
    BOOL            fSuccess;
	const VBDEVICEINFO *pDeviceInfo;
    fSuccess = VbQueryDevice( IPAddress, &deviceInfo );
pDeviceInfo = &deviceInfo;

//    VBMSRV_PLAYBACKINFO pbInfo;
	char Buffer[17];

    sprintf(Buffer,
        "%02X:%02X:%02X:%02X:%02X:%02X",
        pDeviceInfo->macAddress.abMacAddr[ 0 ],
        pDeviceInfo->macAddress.abMacAddr[ 1 ],
        pDeviceInfo->macAddress.abMacAddr[ 2 ],
        pDeviceInfo->macAddress.abMacAddr[ 3 ],
        pDeviceInfo->macAddress.abMacAddr[ 4 ],
        pDeviceInfo->macAddress.abMacAddr[ 5 ]
        );
		return std::string(Buffer);
}
 
I have been doing some homework on htis and feel it is to do with the std::string not being compatable with the windwos system string.
I believe I need to use the system::string and the #included <windows.h>
but am now having troubles compiling
 
Return a string from a C++ dll to vb.net - PInvoke error

I have had major troubles with this one. First of all I needed to work out how to convert char[] to string

Now my string returns the following error when used in VB

PInvokeStackImbalance was detected
Message: A call to PInvoke function 'IVcom!IVcom.Nortronics.Indigo.IVcomObjects::GetMA C' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

I have been doing some homework on this and feel it is to do with the std::string not being compatable with the windows system string.
I believe I need to use the system::string and the #included <windows.h>
but am now having troubles compiling

VB Code:

VB.NET:
Public Declare Function GetMAC Lib "IVserial.dll" (ByVal IPddress As String) As String
     Public Function returnMAC(ByVal mIPaddress As String) As String
            Dim strMAC As String
            strMAC = GetMAC(mIPaddress)
            returnMAC = strMAC
        End Function



c++ Code:
VB.NET:
string __stdcall GetMAC(const char *IPAddress) 
 {
    VBDEVICEINFO    deviceInfo;
    char           *pszIpAddr = NULL;
    BOOL            fSuccess;
	const VBDEVICEINFO *pDeviceInfo;
    fSuccess = VbQueryDevice( IPAddress, &deviceInfo );
pDeviceInfo = &deviceInfo;

//    VBMSRV_PLAYBACKINFO pbInfo;
	char Buffer[17];

    sprintf(Buffer,
        "%02X:%02X:%02X:%02X:%02X:%02X",
        pDeviceInfo->macAddress.abMacAddr[ 0 ],
        pDeviceInfo->macAddress.abMacAddr[ 1 ],
        pDeviceInfo->macAddress.abMacAddr[ 2 ],
        pDeviceInfo->macAddress.abMacAddr[ 3 ],
        pDeviceInfo->macAddress.abMacAddr[ 4 ],
        pDeviceInfo->macAddress.abMacAddr[ 5 ]
        );
		return std::string(Buffer);
}
 
Back
Top