Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8872509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:14:15+00:00 2026-06-14T18:14:15+00:00

So I am trying to pass an integer an a =3 , from Server

  • 0

So I am trying to pass an integer an a =3 , from Server side to Client.The problem is that when i do printf of the message on the Client side, instead of 3 the value displayed is a random number ( something like 19923).I was trying to pass the a by value(&a) at the server side but then the value displayed was a heart shape.Please see what is wrong with that communication.Thanks in advance.

   //Server
    #include <windows.h>
    #define BUF_SIZE 256
    LPSTR szMapName = "MyFileMappingObject";

    int main(void)
    {
        int a = 3;
        HANDLE hMapFile;
        LPVOID lpMapAddress;
        BOOL bRet;

        hMapFile = CreateFileMapping(
            INVALID_HANDLE_VALUE,         /* use swap, not a particular file */
            NULL,                     /* default security */
            PAGE_READWRITE,               /* read/write access */
            0,                        /* maximum object size (high-order DWORD) */
            1024,                     /* maximum object size (low-order DWORD) */
            szMapName);               /* name of mapping object */
        if (hMapFile == INVALID_HANDLE_VALUE){
                 printf("CreateFileMapping error %lu",GetLastError());
            }
        lpMapAddress = MapViewOfFile(
                hMapFile,         /* handle to map object */
                FILE_MAP_ALL_ACCESS,  /* read/write permission */
                0,            /* offset (high-order) */
                0,            /* offset (low-order) */
                0);
        if (lpMapAddress == NULL) 
                 printf("MapViewOfFile error");

        //ZeroMemory(lpMapAddress, strlen(szMsg) + 1);
        CopyMemory(lpMapAddress, a, sizeof(a));

        Sleep(10000);

        bRet = UnmapViewOfFile(lpMapAddress);
        if (bRet == FALSE)
                printf("UnampViewOfFile error");

        bRet = CloseHandle(hMapFile);
        if (bRet == FALSE)
                printf("CloseHandle error");

        return 0;
    }
//Client
#include <windows.h>
#include <stdio.h>
#define BUF_SIZE 256
LPSTR szMapName = "MyFileMappingObject";

int main(void)
{
    HANDLE hMapFile;
    LPVOID lpMapAddress;
    BOOL bRet;
    hMapFile = OpenFileMapping(
        FILE_MAP_ALL_ACCESS,                 /* read/write access */
        FALSE,                               /* do not inherit the name */
        szMapName);                          /* name of mapping object */ 

    if (hMapFile == INVALID_HANDLE_VALUE){
        printf("CreateFileMapping error %lu",GetLastError());
        return 1;
    }

    lpMapAddress = MapViewOfFile(
        hMapFile,       /* handle to map object */
        FILE_MAP_ALL_ACCESS,    /* read/write permission */
        0,          /* offset (high-order) */
        0,          /* offset (low-order) */
        0);

    if (lpMapAddress == NULL){
        printf("MapViewOfFile error %d",GetLastError());
        return 1;
    }

    printf("Message from server is: %d\n", lpMapAddress);

    bRet = UnmapViewOfFile(lpMapAddress);
    if (bRet == FALSE){
        printf("UnampViewOfFile");
        return 1;
    }
    bRet = CloseHandle(hMapFile);
    if (bRet == FALSE){ 
        printf("CloseHandle");
        return 1;
    }

    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:14:16+00:00Added an answer on June 14, 2026 at 6:14 pm

    This is incorrect:

    printf("Message from server is: %d\n", lpMapAddress);
    

    as lpMapAddress is of type LPVOID (a void*) so this will print the memory address to which lpMapAddress points, not an integer value. (Just to note that %p must be used to print a pointer value).

    Based on the technique used to write the int to the shared memory:

    CopyMemory(lpMapAddress, a, sizeof(a));
    

    to extract the int would be the inverse:

    int read_a;
    CopyMemory(&read_a, lpMapAddress, sizeof(read_a));
    printf("Message from server is: %d\n", read_a);
    

    Note that the format specifier is missing from the following line:

    printf("CreateFileMapping error",GetLastError());
    

    should be:

    printf("CreateFileMapping error: %lu", GetLastError());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to pass an integer value like SQL command parameters? I am trying like
I'm having problems trying to pass an Integer object from a driver class as
I am trying pass class as value in hashmap. I need to get the
im trying to pass two parameters to a function, i being an int value
Wondering what I am doing wrong here. I am trying to pass an integer
I'm trying to send the user id (integer) from a view to the action.
hello i am trying to pass parameters from asp LinkButton to a sub routine
Am trying to pass the selected check box value to the control function. Please
I'm trying to pass a string from C# to Delphi built DLL. Delphi DLL
Hey I got a problem here. I'm trying to retrieve the date from the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.