Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27472

VS 2010 Calling functions from a DLL requiring a structure.

$
0
0
Hello,

i'm new here. i'm Pierre, and i'm a C / C++ developper. i have a very little knowledge of VB.
i have a C++ DLL with a few exports, as follow:

Code:

    // Version getter.
    SCLibOpt    void const*        SCCConv    SCVersion          ();
    // Static initializer shortcut.
    SCLibOpt    void                SCCConv    SCStaticInitializer ();
    // Instanciation / Init / Config / Delete.
    SCLibOpt        void*              SCCConv    SCInstanciate                (void const *devConfig);
    SCLibOpt        qint32              SCCConv    SCInitialize                (void *scHndl);
    SCLibOpt        qint32              SCCConv    SCConfigure                        (void *scHndl, void const *config);
    SCLibOpt        void                SCCConv    SCDelete                        (void *scHndl);
    // Api / Infos / Error.
    SCLibOpt        qint32              SCCConv    SCErrorCode                        (void *scHndl, quint8 reset=true);
    // Start.
    SCLibOpt        qint32              SCCConv    SCStartProcessing        (void *scHndl);
    // Stop.
    SCLibOpt        void                SCCConv    SCStopProcessing        (void *scHndl);
    // Query documents.
    SCLibOpt        void                SCCConv    SCDocumentString        (void *scHndl, quint32 docId, quint32 offset, quint8 *outKey, quint8 *outVal);

Those exports are perfectly functional from a test C++ application, and the DLL runs fine.

However, i'm unable to use them with VB.net. i would like to understand how to call them.
i'm able to call the function that don't require parameters (SCStaticInitializer and SCVersion). This works:
Code:

    Private Declare Function SCStaticInitializer Lib "SCLibrary.dll" () As Long
    Private Declare Function SCVersion Lib "SCLibrary.dll" () As String
    Sub Main()
        SCStaticInitializer()
        Console.WriteLine("Statically Initialized.")
        Dim szVersion = SCVersion()
        Console.WriteLine(szVersion)
    End Sub

However, this doesn't:
Code:

    Structure SCDeviceConfig
        Public enableLogging As Byte
        Public deviceId As Byte
        Public dataPath As String
        Public locale As String
    End Structure
    Private Declare Function SCInstanciate Lib "SCLibrary.dll" (ByRef devConf As SCDeviceConfig) As UIntPtr
    Sub Main()
        '' [......]
        Dim devConf As New SCDeviceConfig
        devConf.enableLogging = 1
        devConf.deviceId = 255
        devConf.dataPath = "C:/Some/Path"
        devConf.locale = "EN"
        Dim scHndl = SCInstanciate(devConf)
        Console.WriteLine(scHndl)
    End Sub

A System.AccessViolationException is thrown when calling SCInstanciate...

The structure expected by the DLL is as follow:
Code:

    // DeviceConfig object.
    struct SCLibOpt DeviceConfig {
        quint8 enableLogging;
        quint8 deviceId;
        char *dataPath;
        char *locale;
    };

Can someone please tell me what i'm doing wrong? Is it the VB structure that is not correctly declared (How to put a char* in a structure in VB?)? Or maybe the way the function is declared (Return type memory size, etc?)? i have tried to play around with some GCHandles instead of the Strings in the structure, with no luck neither... i have also tried to change the return type of the function, with no joy.

As i'm more experienced with low-level languages, i would like to understand how to manage memory myself within VB, so i can get more granularity control over my variables.

Thank you very much for your help!
Pierre.

Viewing all articles
Browse latest Browse all 27472

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>