Link to home
Start Free TrialLog in
Avatar of Mobinex
Mobinex

asked on

How to call dll made from VS C++ in VB.NET?

Hi Experts,
I have implemented a dll by using VS C++(2005) ClassLibrary Project as below:

namespace MyTestLib{

      public ref class MyTest
      {
            
            public:
            bool GetString(const char *in, char *out, const unsigned char *key);
        };
}


From VB.NET project I have add reference to my dll.
and I am using my class from dll as below:

Dim myClass As New MyTestLib.MyTest

Dim b As Boolean
Dim sIn As String
Dim sOut As String
Dim sKey As String

sIn = "Test String"
sKey ="Test Key"
b = false

b = myClass.GetString(sIn, sOut, sKey)

It show me error:

Form1.vb(205) : error BC30657: 'GetString' has a return type that is not supported or parameter types that are not supported.


Can you help me what this error and how to solve the problem?
I am not .NET programmer but I want call dll that I made from VS2005 in to vb.NET,
so how to correct my code above to let me can call in vb.NET project?

Please help me soon
Thank in advance.


Avatar of drichards
drichards

Assuming by the "ref" in the class definition and teh fact that you can reference it directly in the VB.NET, this is a managed C++ project, or at least a mixed project.

If that is the case, then char* prevents you from using the function in VB.NET because char* is not a CLS-compliant type.  Change the parameters in your C++ function to System::String^ and, if necessary, convert to and from char* inside the function.
Avatar of Mobinex

ASKER

Yah, Many thank for your help,
I have changed

bool GetString(const char *in, char *out, const unsigned char *key);
to:

bool GetString(System::String^ in, System::String^ out, System::String^ key)
The compile is OK, BUT I have problem with get out put string with code as below:

Dim b As Boolean
Dim sIn As String
Dim sOut As String
Dim sKey As String

sIn = "Test String"
sKey ="Test Key"
b = false
sOut  = ""

b = myClass.GetString(sIn, sOut, sKey)
After this call, sOut still is empty event if in the GetString
I have assigned out param string to the value is "7ac8102hgc7"

So, It seems i have problem about refer the variable ByRef in the function.

Can you help me how to use a ByRef variable for GetString function by using System.String^? or any way to solve my problem (Problem is I want to get string to out put string param is sOut)

Many thank again.
Avatar of Mobinex

ASKER

Oh,
I have added % to System::String^ as System::String^% out  then I can get the string (ByRef)
BUT in my function if I use System::String^
I need to convert char* to String^ so I have question for the code below:

char* sText =NULL;
.......
//Convert char* to String^ by using gcnew

System::String^ sTemp = gcnew System::String(sText);
delete sText ;
sOut = sTemp ;

So, How i can free sTemp that created by gcnew function.
Do we need free sTemp? if we need then can you help me what function to Free sTemp in this case?

Tx
You do not need to free System::String objects as the .NET garbage collection manages that.  In fact there isn't a way for you to free them.  When all references to an object go out of scope, the object is eventually cleaned up by garbage collection - one of the supposed advantages of a managed environment.
ASKER CERTIFIED SOLUTION
Avatar of drichards
drichards

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial