Thread: passing struct to function

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    55

    passing struct to function

    Hello all! I'm creating a Win32 App using MSVC++ 6 compiler.
    I'm looking for help because I don't understand why I am getting these errors. I've been reading up on structs in C ... but it hasn't clicked yet. I'm returning pVect so I can use it later in another function and it will be called more than once. I think i'm doing something wrong with my struct, but I cannot pinpoint it from here...is there a better way to pass a struct or use pointers in a different way...so confusing =[
    Code:
    		struct vect2d
            {
                float h;
                float v;
    
            };
    Code:
    	float getMouse(struct vect2d mVect)
            {	
    			
    			float One0;
    			float One1;
    
    			mVect.h = ReadProcessMemory(hand, (void*)0x402AD4B8, &One0,4, &bytes);//169
    			mVect.v = ReadProcessMemory(hand, (void*)0x402AD4BC, &One1,4, &bytes);//170
                return mVect;//171
            }
    Code:
    main.c(169) : warning C4244: '=' : conversion from 'int ' to 'float ', possible loss of data
    main.c(170) : warning C4244: '=' : conversion from 'int ' to 'float ', possible loss of data
    main.c(171) : error C2115: 'return' : incompatible types
    Last edited by silentkarma; 03-05-2008 at 11:21 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Moved to the Windows programming forum.

    You might want to indent your code more consistently.

    An online search tells me ReadProcessMemory() returns a BOOL (which is a typedef for int, methinks), not a float, so you probably want to write either:
    Code:
    struct vect2d getMouse()
    {
        struct vect2d mVect;
        ReadProcessMemory(hand, (void*)0x402AD4B8, &mVect.h, 4, &bytes);
        ReadProcessMemory(hand, (void*)0x402AD4BC, &mVect.v, 4, &bytes);
        return mVect;
    }
    or:
    Code:
    void getMouse(struct vect2d* mVect)
    {
        ReadProcessMemory(hand, (void*)0x402AD4B8, &mVect->h, 4, &bytes);
        ReadProcessMemory(hand, (void*)0x402AD4BC, &mVect->v, 4, &bytes);
    }
    ... assuming that everything else that you are doing is correct.
    Last edited by laserlight; 03-06-2008 at 02:56 AM. Reason: Typo.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    55
    PERFECT. Exactly what I needed to see! =] Thanks!

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Incidentally, for completeness it may be better to write:
    Code:
    BOOL getMouse(struct vect2d* mVect)
    {
        return ReadProcessMemory(hand, (void*)0x402AD4B8, &mVect->h, 4, &bytes)
            && ReadProcessMemory(hand, (void*)0x402AD4BC, &mVect->v, 4, &bytes);
    }
    I am a little uneasy though, since it looks like hand and bytes are global variables, and I hope you know what you are doing with the addresses.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    55
    I have them declared at the top like so:
    Code:
    DWORD bytes; 
    HANDLE hand = NULL;
    I then find the process on Initilization.. but since it's inside another function, will I need to set hand = OpenProcess each time I want to read/write in a different function?

    Could you explain the significant different between these two functions? What is the end result of both?
    Code:
    BOOL getMouse(struct vect2d* mVect)
    {
        return ReadProcessMemory(hand, (void*)0x402AD4B8, &mVect->h, 4, &bytes)
            && ReadProcessMemory(hand, (void*)0x402AD4BC, &mVect->v, 4, &bytes);
    }
    Code:
    void getMouse(struct vect2d* mVect)
    {	
        ReadProcessMemory(hand, (void*)0x402AD4B8, &mVect->h,4, &bytes);
        ReadProcessMemory(hand, (void*)0x402AD4BC, &mVect->v,4, &bytes);
    }

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The first returns TRUE (ie success) if both ReadProcessMemory succeeds.
    The second does not return anything at all (so it's impossible to know if they failed).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    55
    But I need to to actually return a value of mVect (which i think is int)so I can use it later in a calculation to get an angle distance and then write it back in another function. True or false would do me no good I think.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    mVect is a struct and you're passing a pointer, so the data is indeed written into your struct.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    55
    Awesome! Thanks for the info Elysia and laserlight! Got it working now.

    I'm now doing this successfully:
    Code:
    struct vect2d
    {
    	float h;
    	float v;
    };
    
    void getMouse()
    {
    	struct vect2d mVect;
    	ReadProcessMemory(hand, (void*)0x4BD7D4B8, &mVect.h,4, &bytes);
    	ReadProcessMemory(hand, (void*)0x4BD7D4BC, &mVect.v,4, &bytes);
    }
    
    
    
    void setMouse()
    {
    	struct vect2d mVect;
    	WriteProcessMemory(hand, (void*)0x4BD7AF00, &mVect.h,4, &bytes);
    	WriteProcessMemory(hand, (void*)0x4BD7AF04, &mVect.v,4, &bytes);
    }
    
    //Then in main, call the functions
    Just for clarification, do these mean the same thing?:
    Code:
    void getMouse()
    {
    	struct vect2d mVect;
    	ReadProcessMemory(hand, (void*)0x4BD7D4B8, &mVect.h,4, &bytes);
    	ReadProcessMemory(hand, (void*)0x4BD7D4BC, &mVect.v,4, &bytes);
    }
    Code:
    void getMouse(struct vect2d* mVect)
    {
    	ReadProcessMemory(hand, (void*)0x4BD7D4B8, &mVect.h,4, &bytes);
    	ReadProcessMemory(hand, (void*)0x4BD7D4BC, &mVect.v,4, &bytes);
    }
    Last edited by silentkarma; 03-06-2008 at 02:48 PM.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This does not work as intended.
    The struct you define in the function is destroyed when the function returns, so all the data you just read is lost.
    And you never return whether or not the function succeeded or failed.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    55
    Hmm, interesting...

    Because when I look in memory of that process, that data is updated and writes continuously to the address I specified...so it IS working, but maybe it's not how I should be doing it?

    I've removed everything but this from my program and it's working properly. I check memory and the data IS writing to the address so I'm not sure why you say it's lost?

    Here's what I have that is working:
    Code:
    #include <windows.h>
    
    DWORD pid; 
    HWND hwndWindow; 
    DWORD bytes; 
    HANDLE hand=NULL;
    char *myWindow = "theApp";
    
    
    struct vect2d
    {
    	float h;
    	float v;
    };
    		
    void getMouse()
    {
    	struct vect2d mVect;
    	ReadProcessMemory(hand, (void*)0x4BD7D4B8, &mVect.h,4, &bytes);
    	ReadProcessMemory(hand, (void*)0x4BD7D4BC, &mVect.v,4, &bytes);
    }
    
    
    
    void setMouse()
    {
    	struct vect2d mVect;
    	WriteProcessMemory(hand, (void*)0x4BD7AF00, &mVect.h,4, &bytes);
    	WriteProcessMemory(hand, (void*)0x4BD7AF04, &mVect.v,4, &bytes);
    }
    
    void main(void)
    {
    	hwndWindow = FindWindow(NULL,myWindow);	
    	
    	if(hwndWindow) 
    	{
    			
    		GetWindowThreadProcessId(hwndWindow, &pid);
    		hand = OpenProcess(PROCESS_ALL_ACCESS,0,pid);
    		
    		while(hand!=0)
    		{
    			getMouse();
    			setMouse();
    		}
    	}
    }
    Last edited by silentkarma; 03-06-2008 at 02:59 PM.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I did mention "read," didn't I? All the data you read is lost once the function returns.
    What is the point of reading the memory if you're not going to do anything with it?
    Further, you also write junk data to the process memory, as well, since the struct is uninitialized.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    55
    Quote Originally Posted by Elysia View Post
    I did mention "read," didn't I? All the data you read is lost once the function returns.
    What is the point of reading the memory if you're not going to do anything with it?
    Further, you also write junk data to the process memory, as well, since the struct is uninitialized.
    I do appologize that I wasn't taking into consideration what you said. However, looking over what you suggested, I came up something, tell me if this looks any better.. =p

    Code:
    #include <windows.h>
    
    DWORD pid; 
    HWND hwndWindow; 
    DWORD bytes; 
    HANDLE hand=NULL;
    char *myWindow = "myApp";
    
    
    struct vect2d
    {
    	float h;
    	float v;
    };
    		
    void getMouse(struct vect2d* mVect)
    {
    	ReadProcessMemory(hand, (void*)0x4BD7D4B8, &mVect->h,4, &bytes);
    	ReadProcessMemory(hand, (void*)0x4BD7D4BC, &mVect->v,4, &bytes);
    }
    
    
    
    void setMouse(struct vect2d* mVect)
    {	
    	WriteProcessMemory(hand, (void*)0x4BD7AF00, &mVect->h,4, &bytes);
    	WriteProcessMemory(hand, (void*)0x4BD7AF04, &mVect->v,4, &bytes);
    }
    
    void main(void)
    {
    	hwndWindow = FindWindow(NULL,myWindow);	
    	
    	if(hwndWindow) 
    	{
    		struct vect2d mVect;
    			
    		GetWindowThreadProcessId(hwndWindow, &pid);
    		hand = OpenProcess(PROCESS_ALL_ACCESS,0,pid);
    		
    		while(hand!=0)
    		{
    			getMouse(&mVect);
    			setMouse(&mVect);
    		}
    	}
    }

  14. #14
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by silentkarma View Post
    Code:
     float getMouse(vect2d mVect)
            {    
     
                float One0;
                float One1;
     
                mVect.h = ReadProcessMemory(hand, (void*)0x402AD4B8, &One0,4, &bytes);//169
                mVect.v = ReadProcessMemory(hand, (void*)0x402AD4BC, &One1,4, &bytes);//170
                return mVect;//171
            }

    you dont need the struct keyword in the function parameter list. Once you declare a struct, the name of the struct becomes a type.

  15. #15
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by abachler View Post
    you dont need the struct keyword in the function parameter list. Once you declare a struct, the name of the struct becomes a type.
    In C you do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Acessing a struct after passing to a function
    By bomberto in forum C++ Programming
    Replies: 5
    Last Post: 10-04-2006, 04:29 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. passing struct to function help
    By staticalloc in forum C Programming
    Replies: 4
    Last Post: 10-06-2004, 08:30 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM