Solved

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

Posted on 2008-10-25
5
756 Views
Last Modified: 2012-05-05
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.


0
Comment
Question by:Mobinex
  • 3
  • 2
5 Comments
 
LVL 19

Expert Comment

by:drichards
ID: 22803888
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.
0
 
LVL 1

Author Comment

by:Mobinex
ID: 22804231
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.
0
 
LVL 1

Author Comment

by:Mobinex
ID: 22804263
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
0
 
LVL 19

Expert Comment

by:drichards
ID: 22804853
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.
0
 
LVL 19

Accepted Solution

by:
drichards earned 250 total points
ID: 22804859
You do need to keep the "delete sText;" to free the unmanaged string, however.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now