Pass char * argument and get char * return from a C++ DLL to a VB.NET Application-VBForums
Results 1 to 3 of 3

Thread: Pass char * argument and get char * return from a C++ DLL to a VB.NET Application

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2018
    Posts
    2

    Exclamation Pass char * argument and get char * return from a C++ DLL to a VB.NET Application

    I have a C++ DLL function with the following definition-
    char * scanImage(char * fileDir)
    Code:
    {
    	string dir(fileDir);
    	string cvStr = cvScan(dir);
    	char * scanReturn;
    	scanReturn = (char*)CoTaskMemAlloc((cvStr.size() + 1) * sizeof(char));
    	strcpy(scanReturn, cvStr.c_str());
    	cout << "DLL Output: " << endl << scanReturn <<endl;
    	return scanReturn;
    }
    Headerfile of the DLL is-

    Code:
    #pragma once
    #include <Windows.h>
    #include <string>
    using namespace std;
    
    #ifdef THEIA_OPENCV_DLL_EXPORTS
    #define THEIA_OPENCV_DLL_API __declspec(dllexport)
    #else
    #define THEIA_OPENCV_DLL_API __declspec(dllimport)
    #endif
    
    extern "C"  THEIA_OPENCV_DLL_API char * scanImage(char * fileDir);
    Now I want to call this scanImage function from a VB.NET Application.

    So far I have been trying these-

    Trial 1:

    Code:
    Imports System.Runtime.InteropServices
    Public Class Form1
    
        Public Declare Function scanImage Lib "Theia_OpenCV_Dll.dll" 
        (<MarshalAs(UnmanagedType.LPStr)> fileDir As String) As IntPtr
        
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim out As IntPtr = scanImage("files/stu2/2.bmp")
            Label1.Text = Marshal.PtrToStringAnsi(out)
        End Sub
    
    End Class
    This code changed the default text of label1 to blank. But displays nothing. On second click this terminates the program.

    Trial 2:

    Code:
    Imports System.Runtime.InteropServices
    Public Class Form1
    
        Public Declare Function scanImage Lib "Theia_OpenCV_Dll.dll" (<MarshalAs(UnmanagedType.LPStr)> fileDir As String) As <MarshalAs(UnmanagedType.LPStr)> System.Text.StringBuilder
        
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim sb As System.Text.StringBuilder = scanImage("files/stu2/2.bmp")
            label1.text = sb.ToString
        End Sub
    
    End Class
    This gives me-
    Program_name has stopped working
    and the program terminates immediately.

    I am using VB.NET in Visual Studio 2017.
    Now how to solve this problem?

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    5,580

    Re: Pass char * argument and get char * return from a C++ DLL to a VB.NET Application

    Your Trial 1 code appears to be correct. That being the case, are you sure the C function scanImage is actually working?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2018
    Posts
    2

    Post Re: Pass char * argument and get char * return from a C++ DLL to a VB.NET Application

    Quote Originally Posted by Niya View Post
    Your Trial 1 code appears to be correct. That being the case, are you sure the C function scanImage is actually working?
    Yeah, the C (Actually C++) function is working. And this problem is solved anyway. And you are right, Trial 1 code was right. There was a little problem with DLL path.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.