?
Solved

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

Posted on 2008-10-25
5
Medium Priority
?
771 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 750 total points
ID: 22804859
You do need to keep the "delete sText;" to free the unmanaged string, however.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month10 days, 9 hours left to enroll

764 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