Solved

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

Posted on 2008-10-25
5
750 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
You do need to keep the "delete sText;" to free the unmanaged string, however.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Error when running console app 9 127
L prefix in C++ 8 99
please insert disk adobe master collection6 9 330
Tab order 16 51
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

763 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

7 Experts available now in Live!

Get 1:1 Help Now