Valhalla Legends Forums Archive | .NET Platform | [vb.net]importing c++ dll

AuthorMessageTime
mentalCo.
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.
November 3, 2004, 11:10 PM
Myndfyr
[quote author=mentalCo. link=topic=9413.msg87265#msg87265 date=1099523431]
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.
[/quote]

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.
November 4, 2004, 3:09 AM
mentalCo.
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:

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

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.
November 4, 2004, 8:22 AM
Myndfyr
[quote author=mentalCo. link=topic=9413.msg87325#msg87325 date=1099556560]
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:

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

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?
[/quote]

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:
[code]
namespace UGBDLL
{
public __gc class Functions{
public:
__gc System::Int32 Test(System::String *crap);
};
}
[/code]

That gets rid of any ambiguity in types when working across languages.
November 4, 2004, 6:21 PM
mentalCo.
ok i have a new problem.  heres my class in the dll:

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

and heres the definition of Get_0x51():

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

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:

[code]

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";
}
[/code]
November 4, 2004, 9:34 PM
Myndfyr
[quote author=mentalCo. link=topic=9413.msg87377#msg87377 date=1099604054]
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. 
[/quote]

um....  You pass them....
November 4, 2004, 10:52 PM
mentalCo.
lol.

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

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

String* UGBDLL::Functions::Test(String *data){
Modify(data);
return data;
}
[/code]

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.
November 5, 2004, 12:10 AM
Myndfyr
[quote author=mentalCo. link=topic=9413.msg87416#msg87416 date=1099613445]
lol.

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

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

String* UGBDLL::Functions::Test(String *data){
Modify(data);
return data;
}
[/code]

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.

[/quote]

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:

[code]
(System::String)(*strString) += "123";
[/code]

Again: why are you trying to do it this way instead of just going through normal imports of C functions?
November 5, 2004, 1:12 AM
mentalCo.
[quote]
Again: why are you trying to do it this way instead of just going through normal imports of C functions?
[/quote]

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.
November 5, 2004, 3:32 AM
Myndfyr
[quote author=mentalCo. link=topic=9413.msg87453#msg87453 date=1099625565]
[quote]
Again: why are you trying to do it this way instead of just going through normal imports of C functions?
[/quote]

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.
[/quote]

Learn the language first, please.
November 5, 2004, 7:10 AM
mentalCo.
Im in the middle of failing an intorduction to vb.net class right now lol.
November 5, 2004, 7:31 PM

Search