Valhalla Legends Archive

Programming => General Programming => .NET Platform => Topic started by: mentalCo. on November 03, 2004, 05:10 PM

Title: [vb.net]importing c++ dll
Post by: mentalCo. on November 03, 2004, 05:10 PM
how do you use a c++ dll in vb.net?  ive been googling for like an hour and a half and havent really figured it out.  maybe im just dumb.  i need an example or something i guess or a link to a tutorial or just some place to start.
Title: Re: [vb.net]importing c++ dll
Post by: MyndFyre on November 03, 2004, 09:09 PM
Quote from: mentalCo. on November 03, 2004, 05:10 PM
how do you use a c++ dll in vb.net?  ive been googling for like an hour and a half and havent really figured it out.  maybe im just dumb.  i need an example or something i guess or a link to a tutorial or just some place to start.

Depends on whether or not the C++ is managed or not.  If it *is* managed, then you import it as just another library and have access to any types and functions marked as __gc in the DLL.  If it is not managed, then you import it as a standard C DLL; you can use the Public Declare syntax.  You cannot access C++ classes if they are not managed.
Title: Re: [vb.net]importing c++ dll
Post by: mentalCo. on November 04, 2004, 02:22 AM
ok i got it imported and declared.  now its saying "Unable to find an entry point named Test in DLL UGBDLL.dll."  'Test' is my exported function.

edit: diregard what i said lol. 

ok i made my managed c++ dll:


namespace UGBDLL
{
public __gc class Functions{
public:
int Test(char * crap);
};
}


now i go to my vb.net project and do 'add reference' and add my dll to my project as a COM reference.  Now how do i access my dll in vb.net?

edit:
I got it imported via "imports UGBDLL"
i have access to the class "Functions."  How do i call "Test()"?

edit 2:
ok I got it working now lol thanks.
Title: Re: [vb.net]importing c++ dll
Post by: MyndFyre on November 04, 2004, 12:21 PM
Quote from: mentalCo. on November 04, 2004, 02:22 AM
ok i got it imported and declared.  now its saying "Unable to find an entry point named Test in DLL UGBDLL.dll."  'Test' is my exported function.

edit: diregard what i said lol. 

ok i made my managed c++ dll:


namespace UGBDLL
{
public __gc class Functions{
public:
int Test(char * crap);
};
}


now i go to my vb.net project and do 'add reference' and add my dll to my project as a COM reference.  Now how do i access my dll in vb.net?

Why would you add it as a COM reference?  COM is unmanaged.  When you make it managed, that means that you make it available to .NET.  You would add it as a .NET reference.

Also, you should have:

namespace UGBDLL
{
public __gc class Functions{
public:
__gc System::Int32 Test(System::String *crap);
};
}


That gets rid of any ambiguity in types when working across languages.
Title: Re: [vb.net]importing c++ dll
Post by: mentalCo. on November 04, 2004, 03:34 PM
ok i have a new problem.  heres my class in the dll:


namespace UGBDLL
{
public __gc class Functions{
public:
String* Get_0x51(String *mpqname, String *hashcmd, unsigned long encryptvalue, String *cdkey, String* strProduct);
};
}


and heres the definition of Get_0x51():


String* UGBDLL::Functions::Get_0x51(String *mpqname, String *hashcmd, unsigned long encryptvalue, String *cdkey, String* strProduct){
return "test";
}


ok how do i manipulate the arguments.  like mpqname, hashcmd, etc.  i basically want to pass those values off to a different function to be handled.  for example:



void Test(string *strmpqname){
strmpqname += "123";
}

String* UGBDLL::Functions::Get_0x51(String *mpqname, String *hashcmd, unsigned long encryptvalue, String *cdkey, String* strProduct){
                Test(mpqname);
return "test";
}
Title: Re: [vb.net]importing c++ dll
Post by: MyndFyre on November 04, 2004, 04:52 PM
Quote from: mentalCo. on November 04, 2004, 03:34 PM
ok how do i manipulate the arguments.  like mpqname, hashcmd, etc.  i basically want to pass those values off to a different function to be handled. 

um....  You pass them....
Title: Re: [vb.net]importing c++ dll
Post by: mentalCo. on November 04, 2004, 06:10 PM
lol.

i mean like have Test() modify the variables.  take this for example:


void Modify(String *strString){
strString +="123";
}

String* UGBDLL::Functions::Test(String *data){
Modify(data);
return data;
}


I get the error:
error C2845: '+=' : cannot perform pointer arithmetic on __gc pointer 'System::String __gc *'

i read that you need an object to convert between managed and unmanaged pointers or something like that.  i didnt get it and got lost instantly.

edit: ok if i could get an example of a way to copy the contents of "data" into a char array then id be set.
Title: Re: [vb.net]importing c++ dll
Post by: MyndFyre on November 04, 2004, 07:12 PM
Quote from: mentalCo. on November 04, 2004, 06:10 PM
lol.

i mean like have Test() modify the variables.  take this for example:


void Modify(String *strString){
strString +="123";
}

String* UGBDLL::Functions::Test(String *data){
Modify(data);
return data;
}


I get the error:
error C2845: '+=' : cannot perform pointer arithmetic on __gc pointer 'System::String __gc *'

i read that you need an object to convert between managed and unmanaged pointers or something like that.  i didnt get it and got lost instantly.

edit: ok if i could get an example of a way to copy the contents of "data" into a char array then id be set.


Learn the language.  In order to use the += operator, you have to dereference the variable.  When you += to a string, it thinks you want to add a string to a pointer, which is illegal.  You need to dereference:


(System::String)(*strString) += "123";


Again: why are you trying to do it this way instead of just going through normal imports of C functions?
Title: Re: [vb.net]importing c++ dll
Post by: mentalCo. on November 04, 2004, 09:32 PM
Quote
Again: why are you trying to do it this way instead of just going through normal imports of C functions?

I was just getting an understandning I guess.  I think I got it now.  But dont be surprised to hear from me in the near future lol.  thanks.
Title: Re: [vb.net]importing c++ dll
Post by: MyndFyre on November 05, 2004, 01:10 AM
Quote from: mentalCo. on November 04, 2004, 09:32 PM
Quote
Again: why are you trying to do it this way instead of just going through normal imports of C functions?

I was just getting an understandning I guess.  I think I got it now.  But dont be surprised to hear from me in the near future lol.  thanks.

Learn the language first, please.
Title: Re: [vb.net]importing c++ dll
Post by: mentalCo. on November 05, 2004, 01:31 PM
Im in the middle of failing an intorduction to vb.net class right now lol.