CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    May 2009
    Posts
    140

    Post function returning written address

    hi i have problem with that

    Code:
    
    void* FindVersion(void)
    {
    
    	return (void*)(DWORD*)0x01EA2B00;
    }
    
    
    
    struct cl_funcs_s	ofuncs;
    cl_funcs_s *pfuncs	= (cl_funcs_s*) FindVersion();
    it doesn't return address right, so it crash application

    if i just make
    Code:
    struct cl_funcs_s	ofuncs;
    cl_funcs_s *pfuncs	= (cl_funcs_s*) 0x01EA2B00;
    so it is all ok, but i need function there
    Last edited by Owyn; May 18th, 2009 at 04:08 AM.

  2. #2
    Join Date
    Feb 2009
    Location
    India
    Posts
    444

    Re: function returning memory address

    Just do this inside the function.
    return (void*)0x01EA2B00;
    «_Superman
    I love work. It gives me something to do between weekends.

    Microsoft MVP (Visual C++)

  3. #3
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: function returning written address

    What address is that?
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  4. #4
    Join Date
    May 2009
    Posts
    140

    Re: function returning memory address

    Quote Originally Posted by _Superman_ View Post
    Just do this inside the function.
    return (void*)0x01EA2B00;
    i tried that, crashes too

  5. #5
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: function returning memory address

    You've introduced a "magic" address into your code.

    You can do this sort of weird syntax in the IDE debugger where you see the pointers. Not normal to put it into your code.

  6. #6
    Join Date
    May 2009
    Posts
    140

    Re: function returning memory address

    i know that working with magic addresses is bad but anyway i need it in my code, 2nd variant of my code works fine but i need a function or anything so i can choose magic address depending on conditions

  7. #7
    Join Date
    May 2009
    Posts
    140

    Re: function returning memory address

    i use visual studio 2008

    working code
    Code:
    struct cl_funcs_s	ofuncs;
    cl_funcs_s *pfuncs	= (cl_funcs_s*) 0x01EA2B00;
    non working code

    [os.cpp]
    Code:
    void* FindVersion(void)
    {
    
    	return (void*)0x01EA2B00;
    }
    [main.cpp]
    Code:
    #include "os.cpp"
    
    struct cl_funcs_s	ofuncs;
    cl_funcs_s *pfuncs	= (cl_funcs_s*) FindVersion();
    now i really don't know what is wrong ><

  8. #8
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: function returning memory address

    The memory layout may change every time you modify your source code. Assuming to find a function body at a specific memory location is one of the worst techniques I have ever seen
    If you need to return the address of a function, the use the function name to determine its address:

    Code:
    void somefunc()
    {
    }
    
    void* FindVersion()
    {
       return reinterpret_cast<void*>( somefunc );
    }
    Can you please elaborate what exactly you are doing? Maybe the template method pattern can be applied for a more robust solution.
    - Guido

  9. #9
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: function returning memory address

    Quote Originally Posted by Owyn View Post
    i know that working with magic addresses is bad but anyway i need it in my code, 2nd variant of my code works fine but i need a function or anything so i can choose magic address depending on conditions
    Are you doing C development on embedded systems?
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  10. #10
    Join Date
    May 2009
    Posts
    140

    Re: function returning memory address

    i figured out my problem, i needed int FindVersion(void){}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured