How to [share/ transfer] an array [char/int] between C & C++ programs !!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yithape
    New Member
    • Jan 2010
    • 4

    #1

    How to [share/ transfer] an array [char/int] between C & C++ programs !!

    Hello ,

    I have two differnt functions in C & in C++ . I want my variable in C++ program which is an array of characters & Integer if possible to be send to my c program. I tried using pointer but the moment control comes out of my C++ i loose all data in that array or i get garbage variables.

    Can anyone please guide me for this issue. It will be really great !!
  • newb16
    Contributor
    • Jul 2008
    • 687

    #2
    The error is in line 18 of your c++ program. oh wait? Maybe you show the source code first? But it seems that you either deallocate the array too early or pass entirely wrong pointer.

    Comment

    • yithape
      New Member
      • Jan 2010
      • 4

      #3
      None of above is happening:
      1} deallocating the array too early :: Not possible --> as i wait for "recv" function to execute completely to grab all data bytes received from server in my TCP program. And then i assign the [char *] .So eventually the second possible of assigning wrong Pointer is also void. It can not be possible. However i honestly feel i must get the actual progrm & then u can suggest me the exact issue.

      Thanks for ur reply though !!! i appreciate it..

      Comment

      • johny10151981
        Top Contributor
        • Jan 2010
        • 1059

        #4
        Newb16 get a good point you missed it. Give the code fragmentation. Then all can see what you did.

        There nothing wrong calling a C function from C++ program. All client server application that we develop in C++ have to call the functions of socket.h which is written in C. So you better help us by giving the code fragment that is bothering.

        regards,
        johny

        Comment

        • yithape
          New Member
          • Jan 2010
          • 4

          #5
          Code:
          // This is the Send function for sending the querry
          
          while ( ( nCntSend = send( hClientSocket, szBuffer, nLength, 0 ) != nLength ) )
          		{
          
          		    if ( nCntSend == -1 )
          			{
          				cout << "\n Error sending the data to server" << endl ;
          				break ;
          			}
          			if ( nCntSend == nLength )
          				break ;
          			
          			//pBuffer += nCntSend ;
          			cout << "\n Command Sent to Server is :: " << szBuffer << " "  << endl ;
          			nLength -= nCntSend ;
          
          		}
          
          		Sleep(1000); // wait state time for other CAN3 Device   
                                                             // response
          
          
          	
          // The following is the Receive Function of which i Need qzBuffer Variable to 
          // be transferred to any other c program by pointer/ dll/ or any other method.
          
          
          nLength = recv( hClientSocket, qzBuffer, sizeof( qzBuffer ), 0 ) ;
          
          		if ( nLength > 0 )
          		{
          			qzBuffer[ nLength ] = '\0' ;
          
          			cout << " Bytes Received from the Server ::  " << nLength << endl ;
          		   	cout << "\n  Response From Server is ::  "<< endl ;
          		  	q =0;
          
          		   while (q<nLength)
          		   {
          			char b_char = qzBuffer[q];
          			tst[q] = b_char;
             			q = q++;
          		   }
          ----------------------------------------------------------------------------------------------------------------

          Is there any way which makes this data transfer between C & C++ variable or else i was planning for writing the data in .txt or .xls file & then sharing it in C program but. Technically its the worst design i can make.

          So please have your say upon my issue.

          Thnx

          Comment

          • johny10151981
            Top Contributor
            • Jan 2010
            • 1059

            #6
            I have two differnt functions in C & in C++ . I want my variable in C++ program which is an array of characters & Integer if possible to be send to my c program. I tried using pointer but the moment control comes out of my C++ i loose all data in that array or i get garbage variables.

            Can anyone please guide me for this issue. It will be really great !!
            hi yithape,
            Now you have give the source fragmentation. But you post the code wrong didnt followed the convention as well as you didnt mention at which line you have discovered that your data is missing. and what data is missing. please give us the line number as well as also mention the type of qzbuffer.
            i mean how did you declared..
            char *qzbuffer; //or

            char qzbuffer[xxxx];


            regards,
            johny

            Comment

            • johny10151981
              Top Contributor
              • Jan 2010
              • 1059

              #7
              Hi,
              I have asked you the declaration of
              qzBuffer
              you didnt answer. make sure qzBuffer has memory allocation and do not use
              sizeof(qzBuffer ) if qzbuffer is a pointer. use literal value.

              and make sure that by send() function you are sending correct data to the other end of the net work.

              use strcpy or strncpy or any other string copy function it will reduce amount of code.

              these are suggestions. you can accept it or not.

              regards
              johny

              Comment

              Working...