Share Data between C & C++ Function

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

    Share Data between C & C++ Function

    // This is My Client TCP program in C++
    // Program Name client.cpp

    #include <winsock2.h>
    #include <iostream.h>

    #include <windows.h>
    #include <iomanip>


    char szServerIPAddr[ 20 ] = "192.168.5. 251" ; // IP address of my Server
    int nServerPort = 5000 ; // The server port that will be used by clients for communication

    int var1;
    bool InitWinSock2_0( ) ;
    int main( )

    {

    unsigned char str[12] =
    {0xAA,0xFF,0xC0 ,0x00,0x64,0x07 ,0xB7,0x98,0x2F ,0x4D,0x08,0x02 };

    // Querry Request to the Server

    char tst[12] ={0};
    char szBuffer[12] = {0};
    char qzBuffer[12] = {0};

    int q = 0;

    while (q<12)
    {
    szBuffer[q] = str[q];
    q = q++;
    }

    q=0;

    printf("\n \n Actual Data Bytes To Be Send \n \n ");

    while(q<12)
    {
    printf(" %c \n", szBuffer[q]);
    q = q++;
    }

    Sleep(1000);


    if ( ! InitWinSock2_0( ) )
    {
    cout << "Unable to Initialize Windows Socket environment" << WSAGetLastError ( ) << endl ;
    return -1 ;
    }

    SOCKET hClientSocket ;
    hClientSocket = socket( AF_INET, // The address family. AF_INET specifies TCP/IP
    SOCK_STREAM, // Protocol type. SOCK_STREM specified TCP
    0 // Protoco Name. Should be 0 for AF_INET address family
    ) ;

    if ( hClientSocket == INVALID_SOCKET )
    {
    cout << "Unable to create Server socket please verify the proper connection" << endl ;
    // Cleanup the environment initialized by WSAStartup()
    WSACleanup( ) ;
    return -1 ;
    }

    //*************** *************** *************** *************** *********

    // Create the structure describing various Server parameters


    struct sockaddr_in serverAddr ;
    serverAddr . sin_family = AF_INET ; // The address family. MUST be AF_INET
    serverAddr . sin_addr . s_addr = inet_addr( szServerIPAddr ) ;
    serverAddr . sin_port = htons( nServerPort ) ;


    //*************** *************** *************** *************** **********
    // For Testing

    Sleep(5000); // this command will cause the Delay in Milliseconds

    //*************** *************** *************** *************** **********

    // Connect to the server

    if ( connect( hClientSocket, ( struct sockaddr * ) &serverAddr, sizeof( serverAddr ) ) < 0 )
    {
    cout << "\n \n Unable to connect to " << szServerIPAddr << " on port " << nServerPort << endl ;
    closesocket( hClientSocket ) ;
    WSACleanup( ) ;
    return -1 ;
    }

    int a = 1;

    /*do
    {
    cout << "\n \n Enter '1' to Start '0' to Wait: ";
    cin >> a;

    }
    while(a==0);
    */

    while (a==1)
    {
    int nLength = sizeof( szBuffer ) ;

    cout << "\n \n Actual Buffer Length : " << nLength << "\n \n " ;


    int nCntSend = 0 ;

    // char *pBuffer = szBuffer ;

    Sleep(5000);

    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 Mitsubishi FX2N is :: " << szBuffer << " " << endl ;
    nLength -= nCntSend ;

    }

    Sleep(1000); // Request Time Out Settings for FX2N

    nLength = recv( hClientSocket, qzBuffer, sizeof( qzBuffer ), 0 ) ;

    if ( nLength > 0 )
    {
    qzBuffer[ nLength ] = '\0' ;

    cout << " Bytes Received from PLC :: " << nLength << endl ;

    cout << "\n \n Response From Mitsubishi FX2N is :: "<< endl ;

    q =0;

    while (q<nLength)
    {
    char b_char = qzBuffer[q];
    tst[q] = b_char;
    q = q++;
    }

    q=0;
    printf("\n \n Actual Data Bytes Received \n \n ");


    while(q<nLength )
    {
    unsigned char c_char = tst[q];
    short int p = c_char;
    printf(" %d \n", p);
    q = q++;
    }
    Sleep(5000);

    }

    }
    closesocket( hClientSocket ) ;
    WSACleanup( ) ;
    return 0 ;
    }

    bool InitWinSock2_0( )
    {
    WSADATA wsaData ;
    WORD wVersion = MAKEWORD( 2, 0 ) ;
    if ( ! WSAStartup( wVersion, &wsaData ) )
    return true ;
    return false ;
    }



    // And this is my C program which will use the reply received form the server which is stored in qzBuffer in above clinet.cpp

    // This is C program.

    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <MyHeader.h>

    #ifdef VMS
    #pragma nostandard
    noshare
    #pragma standard
    unsigned short int D[300 + 1];



    int user_server_upd ate(unsigned char * tzBuffer)

    {
    int i, n,m;

    m = n;
    while(m == n)
    {

    for (i=0;i< 12; i++)
    {
    D[i]++;
    }
    n=1;
    m=2;
    }

    D[20]++;

    return (0);
    }



    int main()

    {
    unsigned char T[30] = {0};
    FILE *fp;
    int m;


    m = user_server_upd ate(T);



    if((fp = fopen("c:\\serv er_values.txt", "wb"))==NUL L) {
    printf("Cannot open file.\n");
    exit(1);
    }

    // write the entire array in one step
    if(fwrite(T, sizeof T, 1, fp) != 1) {
    printf("Write error.\n");
    exit(1);
    }

    fclose(fp);
    return;
    }


    // Also i wud be happy to understand any option of DLL usage for sharing the data between C & C++ Function
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You have to be certain that any C++ functions called by your C code are marked as extern "C" in the C++ code:

    Code:
    extern "C" void AFunction()
    {
    
    }
    Otherwise, the compiler will mangle the fuinction name making it different from the function name in the C code.

    As far as data goes, there should be no problem.

    BTW: Please use code tags.

    Comment

    Working...