Solved

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

Posted on 2008-10-25
5
768 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 250 total points
ID: 22804859
You do need to keep the "delete sText;" to free the unmanaged string, however.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

705 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