|
-
July 24th, 2008, 05:44 PM
#1
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
-
July 24th, 2008, 06:15 PM
#2
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.
-
July 25th, 2008, 07:03 AM
#3
Re: Some theoretical reason?
>
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|