Need help, may be easy for you , I'm getting nuts.

M

Marty

Hi,

I have this DLL made with VB.NET made as a Class Library project. I
know this is managed code.

I have my other made with VC++.NET, unmanaged code.

The point is that I want to use function within my DLL in the VC++ project.

I've been reading document over the internet and this
http://msdn.microsoft.com/msdnmag/issues/05/01/CQA/default.aspx

but I can't find a tutorial explaining how to integrate and use my
VB.NET DLL in VC++. There is text that says that I should import the
DLL content but it is not clear for me.

Does any of you would have a good reference to suggest to me?

I appreciate, thank you

Marty
 
T

Tamas Demjen

You have to write a mixed-mode DLL in C++ ( I don't think you can use
any other language, but I may be wrong here). I've done this before. The
mixed-mode DLL exports a pure C interface, and the C functions are
calling into the managed interface using Managed C++ (or C++/CLI once
VC++ 2005 is out).

extern "C"
{
__declspec(dllexport) bool __stdcall Convert([...])
// unmanaged function exported from the DLL
{
try
{
[...] just write your managed code here
return true;
}
catch(...)
{
}
return false;
}
}

You just have to solve the data marshalling, converting managed arrays
to unmanaged, and so on. It depends what you want to pass to the
function and what you want to return. For example, managed data can be
pinned and the pinned pointer passed to an unmanaged function:

MemoryStream __gc* managed_stream = new MemoryStream;
[...]
unsigned char managed_array __gc[] = managed_stream->GetBuffer();
unsigned char __pin* ptr = &managed_array[0];
unmanaged_callback(ptr, static_cast<unsigned>(managed_stream->Length));

You could be more specific if you have trouble with a certain detail.

Tom
 
M

Marty

Hi Tamas,

Thank you for your infromation.

It all start from there, I wanted to add a socket handler in my VC++
program, I've tried with this code that I got from the VC++ help file
(at end of email).

But I couldn't make it work, so I thaught to use the one I already made
in VB.NET, so I get to write my previous email.

What is better or easier? To integrate the code just below to my VC++
project?

Thanks for your help.

Marty



void main() {
try {
// Set the TcpListener on port 13000.
Int32 port = 13000;
IPAddress* localAddr = IPAddress::parse(S"127.0.0.1");

// TcpListener* server = new TcpListener(port);
TcpListener* server = new TcpListener(localAddr, port);

// Start listening for client requests.
server->Start();

// Buffer for reading data
Byte bytes[] = new Byte[256];
String* data = 0;

// Enter the listening loop.
while (true) {
Console::Write(S"Waiting for a connection... ");

// Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
TcpClient* client = server->AcceptTcpClient();
Console::WriteLine(S"Connected!");

data = 0;

// Get a stream Object* for reading and writing
NetworkStream* stream = client->GetStream();

Int32 i;

// Loop to receive all the data sent by the client.
while (i = stream->Read(bytes, 0, bytes->Length)) {
// Translate data bytes to a ASCII String*.
data = Text::Encoding::ASCII->GetString(bytes, 0, i);
Console::WriteLine(String::Format(S"Received: {0}",
data));


// Process the data sent by the client.
data = data->ToUpper();

Byte msg[] = Text::Encoding::ASCII->GetBytes(data);

// Send back a response.
stream->Write(msg, 0, msg->Length);
Console::WriteLine(String::Format(S"Sent: {0}", data));
}

// Shutdown and end connection
client->Close();
}
} catch (SocketException* e) {
Console::WriteLine(S"SocketException: {0}", e);
}

Console::WriteLine(S"\nHit enter to continue...");
Console::Read();
}
 
M

Marty

ok, I finally find out to make it compile without erros, the first
bottleneck is passed.

Have a good day.

Marty
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top