I am trying to write a program that interfaces with a BlackBerry device plugged in via USB (utilizing the Desktop Manager API). The only exampes I can find for doing this are in C++, but I only know how to write/interpret VB .NET (I'm a novice, really), so I decided the easiest way to do this would be to just pass a string to and from the device.

To accomplish this, I thought the easiest thing to do would be to adapt the sample C++ application that comes with the BlackBerry JDE (which demonstrates passing a string to an app on the device using the DM API) to a MFC DLL which I would then call from my VB .NET app.

The main problem I am having is figuring out how to accept or return a string between the DLL and the VB .NET app. Every time I try to pass a string it gives me an error. Research has lead me to understand that this is because of "incompatibility" between the string types of the C++ std::string and the VB .NET String variable types.

Here is my code:
C++

// InterfaceStack.cpp : Defines the initialization routines for the DLL.
//

#include "stdafx.h"
#include "InterfaceStack.h"
#include <string>
#include <cstring>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CInterfaceStackApp

BEGIN_MESSAGE_MAP(CInterfaceStackApp, CWinApp)
END_MESSAGE_MAP()


// CInterfaceStackApp construction

CInterfaceStackApp::CInterfaceStackApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}


// The one and only CInterfaceStackApp object

CInterfaceStackApp theApp;


// CInterfaceStackApp initialization

BOOL CInterfaceStackApp::InitInstance()
{
	CWinApp::InitInstance();

	return TRUE;
}


std::string SendStringToDevice(std::string _StringToSend)
{
	// Code which attempts to seng _StringToSend to the device
	std::string _SendAttemptStatus;

	_SendAttemptStatus = "0";
	return _SendAttemptStatus;
}

std::string ReceivedFromDevice()
{
	std::string _TextReceived;
	_TextReceived = "StringFromDevice";
	return _TextReceived;
}

And here is a sample VB .NET app that imports the MFC DLL functions (for some reason "Add Reference" wouldn't work, but this does if I make the C++ use int/integer instead of std::string/String), then tries to pass a string to the SendStringToDevice function.
VB .NET

Public Class form_Main

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim x As String
            ' Send a string
            x = SendStringToDevice("Hello world")
            MsgBox(x.ToString)

            ' The response
            x = ReceivedFromDevice()
            MsgBox(x.ToString)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    <System.Runtime.InteropServices.DllImport("InterfaceStack.dll", SetLastError:=False)> _
    Public Shared Function SendStringToDevice(ByVal _StringToSend As String) As String

    End Function

    <System.Runtime.InteropServices.DllImport("InterfaceStack.dll", SetLastError:=False)> _
    Public Shared Function ReceivedFromDevice() As String

    End Function

End Class

I believe that I have the Java-based on-device app working fine (it listens for a string via USB) because I adapted the program from the JDE sample.

Read this article about how to pass strings between VB and c++

Read this article about how to pass strings between VB and c++

Thanks! Problem solved. :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.