How to convert managed class to unmanaged char* in vc++.net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • selvialagar
    New Member
    • Apr 2008
    • 57

    How to convert managed class to unmanaged char* in vc++.net

    i'm new to vc++.net...I'm doing a project to send a packet to another machine like ping command...in this i'm using sendto() function..i have to convert all the class parametes to char*...
    my code is ..
    Code:
    int	SendEchoRequest(SOCKET s, LPSOCKADDR_IN lpstToAddr)
    	   {
    		   ECHOREQUEST &echoreq=*new ECHOREQUEST;
    		   static nID=1;
    		   static nSeq=1;
    		   int nRet;
    		   echoreq.IcmpHDR->Type=ICMP_ECHOREQ;  
    		   Console::WriteLine(S"Type :{0}",echoreq.IcmpHDR->Type.ToString());    
    		   echoreq.IcmpHDR->Code=0; 
    		   echoreq.IcmpHDR->Checksum=0;  
    		   echoreq.IcmpHDR->ID=nID++;
    		   echoreq.IcmpHDR->Seq_no=nSeq++;
    		   for(nRet=0;nRet<REQ_DATASIZE;nRet++)
    		   {
    			   echoreq.Cdata[nRet]=' '+nRet;
    		   }
    		   echoreq.dwTime=GetTickCount();
    		   const char *buffer;
    		   int length;
    		   System::Runtime::InteropServices::Marshal::Copy(echoreq,0,buffer,length);    
    		   //echoreq->IcmpHDR->Checksum =in_cksum((unsigned short *)&echoreq,echoreq->ToString()->get_Length());
    		   nRet=sendto(s,buffer,length,0,(LPSOCKADDR)lpstToAddr,sizeof(SOCKADDR_IN));
    		   if (nRet == SOCKET_ERROR) 
    		       ReportError("sendto()");
    	        return (nRet);
    	   }
    i tried using marshall copy function but it wont work....what i'll do to correct this problem....
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    What do you need a char* for now? I didn't see any unamanaged code in there?

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      What do you need a char* for now? I didn't see any unamanaged code in there?
      Was it that sendto() function? Why not just use .NET sockets?

      Comment

      Working...