Re: Does any one ever use function capFileSaveDIB?
On Wed, 12 Mar 2008 19:29:32 -0700 (PDT), Andrew Chang
<laplacezhang@gmail.com> wrote:
I use VS .NET 2003. I had connected to a USB camera. I tried to
capture the image. The function capFileSaveDIB is used. MSDN said this
function would return true if successful. I call the function like
this:
It's not a function, it's a macro for a Windows message, and to understand
how to use it, you have to look up the message, WM_CAP_FILE_SAVEDIB.
int cameraCapture(HWND hWndofCap, CString pathCStr)
{
char* pathStr = "";
strcpy(pathStr, pathCStr.GetBuffer());
pathCStr.ReleaseBuffer();
This is wrong for several reasons:
1. The variable pathStr points to the first character of a string literal,
and it is undefined to write to string literals, which is what you're doing
with strcpy. I'm surprised you didn't get an access violation.
2. You don't need to use GetBuffer/ReleaseBuffer. You can just use
pathCStr, and CString's operator LPCTSTR will perform the conversion to
char* in an ANSI program.
3. In a Unicode program, CString will be Unicode and consist of an array of
wchar_t, not char, and you can't copy it with strcpy. In fact, your code
won't compile.
if (capFileSaveDIB(hWndofCap, pathStr))
return 1;
else
return 0;
}
I get the return value: 1. However, I don't get the picture where
pathCStr pointed. I don't understand.
Have you met this problem? I can't find out where the error is.
Thank You!
Reviewing the documentation for WM_CAP_FILE_SAVEDIB, I find:
wParam = (WPARAM) 0;
lParam = (LPARAM) (LPVOID) (LPSTR) (szName);
The initial cast to LPSTR indicates you need to provide it with a
char-based string. While the documentation doesn't state it, presumably
Windows treats szName as a constant string, so you should be able to pass
plain old CString in ANSI programs or CStringA in either ANSI or Unicode.
(I don't know why they have that middle cast to LPVOID; it doesn't
accomplish anything.) If you wanted to play it safer, this would be the
time to use GetBuffer/ReleaseBuffer, and if you wanted to rule out any
possibility of Windows messing with your string, copy it to a vector<char>,
CArray, or in recent versions of VC++, CStringA. In a Unicode program, if
you wanted to control the code page for the conversion from wide to narrow
string, I believe you'd have to use WideCharToMultiByte.
--
Doug Harrison
Visual C++ MVP