How to convert structure from managed to unmanaged type

G

Guest

Esteemed Developers
I am trying to pin a structure, but compiler does not execute this statement. Can you kindly check where could be the problem is? (I use Visual C++ .NET Standard

Form1.
--------------------------------------------------------------------------------
public __gc class Form1 : public System::Windows::Forms::For
{
public
....
int retBindVal
SOCKET ServerSocket, ClientSocket
sockaddr_in ServerSockAddrIn, ClientSockAddrIn
.....

-------------------------------------------------------------------

=================================================================
Form1.CP
-------------------------------------------------------------------
void Form1::Start_Server(

...
sockaddr_in __pin * pinned_ServerSockAddrIn; //This statement is nor executed. It jumps to next statement directl
pinned_ServerSockAddrIn = &ServerSockAddrIn
retBindVal = bind ( ServerSocket, (struct sockaddr*) &pinned_ServerSockAddrIn, sizeof(*pinned_ServerSockAddrIn) )
...

========================================================
I tried __pin at structure but it did not work,too. My code was as follows
sockaddr_in __pin * pinned_ServerSockAddrIn
pinned_ServerSockAddrIn = &__box(ServerSockAddrIn)

the compiler gave the following error message:
error C3627: Only a value type can be boxe
 
D

David Olsen

Alper said:
sockaddr_in __pin * pinned_ServerSockAddrIn; //This statement is nor executed. It jumps to next statement directly
pinned_ServerSockAddrIn = &ServerSockAddrIn;

The first statement is skipped because it is just a declaration that
doesn't contain any code to execute. The variable isn't actually
initialized until the following statement. I would normally combine the
declaration and the initialization into a single statement:

sockaddr_in __pin * pinned_ServerSockAddrIn = &ServerSockAddrIn;

This won't change the behavior of your code at all, so if you are having
problems, the problems are somewhere else.
 
G

Guest

Hello David
Thank You very much for your response.
I have tried your recommendation before. As you mentioned that the problem seems to be somewhere else. Problem is at the bind() and then, of course, at the listen() functions. When I use the following code, the bind() returns Error#10047 that is "Address family not supported by protocol family". The code is as follows

sockaddr_in __pin * pinned_ServerSockAddrIn = &ServerSockAddrIn
retBindVal = bind(ServerSocket, (struct sockaddr*)&pinned_ServerSockAddrIn, sizeof(*pinned_ServerSockAddrIn) )

I found a solution for the "struct" casting. At this code, there is not any __pin. Therefore the first line above is omitted. The working code is as follows

retBindVal = bind(ServerSocket, (struct sockaddr*)&pinned_ServerSockAddrIn, sizeof(*pinned_ServerSockAddrIn) )

I would like to thank your consideration, again.
 
G

Guest

Hello David
I notices that I wrote the wrong code to my previous response
Working code is as follows

retBindVal = bind(ServerSocket, (LPSOCKADDR)&ServerSockAddrIn, sizeof(ServerSockAddrIn));
 

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