|
-
Dec 6th, 2002, 09:51 AM
#1
Thread Starter
Lively Member
-
Dec 6th, 2002, 10:06 AM
#2
Frenzied Member
Code:
BSTR WINAPI FormatID(HWND hWnd, LPCSTR sLine)
{
char *tmp = new char[strlen(sLine)]; <- local variable
int n;
n = <to be calculated>;
strcpy(tmp, sLine);
return BSTR(tmp[n]); <- you return a local variable
}
If you return a local volatile variable to the calling function, the memory the variable lived cannot be guaranteed to there.
Volatile means it goes away when the function returns.
Try:
Use a Global variable
Pass a pointer to the return value into the funtion
Use a static pointer in the function and malloc (one time only)
memory. You have to clean up on exit with atexit()
-
Dec 6th, 2002, 10:30 AM
#3
Thread Starter
Lively Member
My code looks like :
#include <windows.h>
char *rtn;
BOOL APIENTRY DllMain(HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved)
{
return TRUE;
}
BSTR WINAPI FormatID(HWND hWnd, LPCSTR sLine)
{
//char *tmp = new char[strlen(sLine)];
int n;
n =<to be calculated>;
strcpy(rtn, sLine);
return BSTR(rtn[n]);
}
Sorry jim mcnamara could you show me the rest of what you were saying.
Cheers.
-
Dec 6th, 2002, 11:11 AM
#4
What's wrong with passing in a buffer for the character you want to get back?
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Dec 7th, 2002, 10:26 AM
#5
You're casting the UNICODE value of the n'th character to a pointer and return this. You have a huge memory leak. I think that's reason enough for failure...
Code:
BSTR WINAPI FormatID(HWND hWnd, LPCSTR sLine)
{
// the tmp is completly unnecessary
int n;
n = <to be calculated>;
LPWSTR str;
int iNeed = MultiByteToWideChar(CP_ACP, 0, sLine+n, 1, NULL, 0);
str = new WCHAR[iNeed];
iNeed = MultiByteToWideChar(CP_ACP, 0, sLine+n, 1, str, iNeed);
if(iNeed == 0)
{
// ERROR!
}
BSTR out = SysAllocString(str);
delete[] str;
return out;
}
This is the only correct way to return a BSTR to VB.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 9th, 2002, 04:42 AM
#6
Thread Starter
Lively Member
Many Thanks CornedBee
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
|