Thread: passing filehandles among classes

  1. #1
    Unk
    Guest

    passing filehandles among classes

    I'm trying to open a file in the client and pass the filehandle to the server (where the file is read).
    I'm having problems using the filehandle in the server as it is invalid in the server.
    Does anyone know how I could solve this prob or if I'm doing anything wrong?

    here is what I have ...
    IN CLIENT ...
    int filehandle;
    filehandle = _open(filename, _O_RDONLY);
    x(filehandle); // x is some function in the server
    _close(filehandle);

    IN SERVER ...
    _read(filehnadle, ...);

  2. #2
    Unk
    Guest
    Thanks for your reply.

    well, I have a COM dll (in-process server) and a client calling the dll functions.

    I open the file in the client and pass the file handle (of type int) to the dll where the file is read. Still can't figure out why it is invalid in the dll (since the dll runs in the same process as its client).

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Did you check the return value of the open function?
    Have you tried to read the file in your client in stead of the server (just to test the reading part)?

  4. #4
    Unk
    Guest
    yes I did.
    If I open and read either in the client or dll its fine.

    Also I notice if I try to open the file again in the dll, the returned filehandle value is 3 (where it should be 4 as I'm opening it the second time).

    Also, its a COM dll ... if that makes a difference.

    thanks.

  5. #5
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    What error do you get when reading the file?
    Code:
    if((bytes_read = _read(filehandle, buffer, count)) <= 0)
    {
       perror("Could not read file");
       /* error handling */
    }

  6. #6
    Unk
    Guest
    I get -1 (meaning invalid filehandle)

  7. #7
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Are you sure its a normal in-process com object...or could it be a proxy for a local server or something?

    Also, why pass a file handle to a com object? Pretty unportable to other languages....maybe better not going to the trouble of a com object for this and just house a simple C++ class in the dll

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing from functions in classes to another function in another class?
    By Robert_Sitter in forum Windows Programming
    Replies: 1
    Last Post: 12-13-2005, 07:46 AM
  2. passing between classes
    By FoodDude in forum C++ Programming
    Replies: 0
    Last Post: 11-11-2005, 02:21 PM
  3. Passing Template values on to other classes
    By vinsta18 in forum C++ Programming
    Replies: 5
    Last Post: 10-20-2004, 05:26 PM
  4. Passing an array of classes
    By prog-bman in forum C++ Programming
    Replies: 2
    Last Post: 10-04-2004, 03:50 AM
  5. Passing classes question
    By Daggie in forum C++ Programming
    Replies: 3
    Last Post: 04-07-2003, 11:09 AM