CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2008
    Posts
    15

    Resolved Some theoretical reason?

    Hi all:

    Trying to follow this tutorial http://www.codeproject.com/KB/COM/com_in_c1.aspx I'v got this code:

    // MS VCpp console application

    long SetString (char*); // function prototype
    typedef long SetStringPtr(char*);
    long GetString(char*, long); // function prototype
    typedef long GetStringPtr(char*, long);

    typedef struct {
    SetStringPtr* SetString;
    GetStringPtr* GetString;
    } IExampleVtbl;

    typedef struct {
    IExampleVtbl* lpVtbl;
    DWORD count;
    char buffer[80];
    } IExample;


    int _tmain(int argc, _TCHAR* argv[]) {

    IExample* example;
    example = (IExample*)GlobalAlloc(GMEM_FIXED, sizeof(IExample));

    // init members
    example->count = 1;
    example->buffer[0] = 0;
    example->lpVtbl->SetString = SetString; // Throw exception!!
    example->lpVtbl->GetString = GetString;

    HGLOBAL hres = GlobalFree((HGLOBAL) example);
    system ("PAUSE");
    return EXIT_SUCCESS;
    }

    long SetString (char* str) { // definition
    return (0);
    }

    long GetString(char* buffer, long length) { // definition
    return(0);
    }

    At run-time there a an exception in the commented line.

    There is some theoretical justification that can not be made the initialization of that function-pointer in that place?

    Thanks in advance

  2. #2
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: Some theoretical reason?

    lpVtbl is not pointing to a valid mmeory location since you haven't allocated anything for it. All you hvae done is allocated for the IExample. Just as you did this:
    IExample* example;

    You need to do similar thing for pointer members of IExample too.

  3. #3
    Join Date
    Jul 2008
    Posts
    15

    Re: Some theoretical reason?

    >
    Quote Originally Posted by kirants
    lpVtbl is not pointing to a valid mmeory location since you >haven't allocated anything for it. All you hvae done is allocated for the >IExample. Just as you did this:
    >IExample* example;

    >You need to do similar thing for pointer members of IExample too.
    Thanks a lot. That unveil the mystery to me!!

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