|
Can anyone tell me why what seems like a simple cast fails to compile in one case, but passes when the code is rearranged.
Let me show you what I mean.
IA and IB are my own COM interfaces with one method taking the other as a parameter.
This fails:
<font color=green>
IAPtr PB = NULL;
IBPtr pA = NULL;
<font color=green>
..
pB->SomeMethod(&pA); <font color=green>
I get the error message:
General.cpp(319) : error C2664: 'MyLibrary::IA::SomeMethod' : cannot convert parameter 1 from '_com_ptr_t<_IIID>::Interface ** ' to 'IUnknown ** '
But This works:
<font color=green>
IAPtr pB = NULL;
IBPtr pA = NULL;
IUnknown* pUnk = NULL;
<font color=green>
..
pUnk = static_cast<IUnknown*>(pA)
pB->SomeMethod(&pUnk);
Surely these equate to the same thing?
I Dream of Absolute Zero
|
|
|
|
|
Actually, they don't.
The pointer pA points to a pointer to the interface class IA , which, according to COM rules, inherits from IUnknown , either directly or indirectly. However, it looks like you use the COM helper classes to declare IAPtr and IBPtr . When you do this, they can no longer be directly cast, but require explicit casting. To skip using COM helper classes, define the pointers in the following manner:
typedef IAPtr IA* After this, you can no longer directly use them, but must instantate and initialize them manually. The prize is that they support direct casting.
The method you describe assumes a pointer to the IUnknown interface pointer. What you're passing it is a pointer to a pointer to IA . These two are not automatically compatible.
Using the following call would solve the case without extra work:
pB->SomeMethod( &(reinterpret_cast<IUnknown*>(pA)) ); -Antti Keskinen
----------------------------------------------
The definition of impossible is strictly dependant
on what we think is possible.
|
|
|
|
|
Thanks for that explanation.
Can't say I fully understand, but when it comes to COM, who does?
I do get the general jist though.
Antti Keskinen wrote:
Using the following call would solve the case without extra work:pB->SomeMethod( &(reinterpret_cast<iunknown*>(pA)) );
I had tried something similiar :
pB->SomeMethod(reinterpret_cast<IUnknown**>(&pA));
But that didn't compile, so I gave up on that train of thought
Thanks again.
I Dream of Absolute Zero
|
|
|
|
|
Hi!
I got data stored in CString variable. The data consist of mulitiline numbers each number in new line. The length of the CString depends on the data and is various. How can I delete e.g. 5 lines from the end of the CString?
Thanks in advance.
|
|
|
|
|
Answer: basic math, pen and a paper.
Following your description, if you'd need to delete the last 5 lines, this means that you'd need to delete so many characters starting from the end that you'd reach the fifth '\n' character. The lines in a CString object are stored sequentically, with line changes denoted as '\n' and the end of the string as '\0'.
An example. Consider that your arbitary string is structured as follows "111111111111\n22222222222\n333333333333\n44444444444\n555555555555\n6666666666666\n77777777777\0".
Now, if you'd need to delete the last two lines, you'd first need to determine how many lines there are on the string. This means finding how many '\n' characters are found. The line quantity is this added with 1 (last line doesn't have a '\n'). When you had this amount, in this case, 7, and would need to delete 2 lines, it would leave us 4 '\n' characters, right ? So, let's search until we find the fifth '\n' character. After we get it's index, we delete characters from the string, starting from this index. Thus, the last fifth '\n' is replaced by a '\0', and all remaining characters are deleted.
Finding the amount of '\n' is easy: start from the beginning, find the first one. Then start again, from the index returned by the first search plus one. By using this recursive search method, you can run through the string and find out how many line changes you have.
It's pretty straigthforward, although might feel difficult at first. But the quicker you start, the quicker you get it done
-Antti Keskinen
----------------------------------------------
The definition of impossible is strictly dependant
on what we think is possible.
|
|
|
|
|
Your idea is pretty good. Thanks!!
|
|
|
|
|
hope this helps...
==>From MSDN:
Deletes a character or characters from a string starting with the character at the given index.
int Delete(
int iIndex,
int nCount = 1
);
Parameters
iIndex
The zero-based index of the first character in the CStringT object to delete.
nCount
The number of characters to be removed.
Return Value
The length of the changed string.
Remarks
If nCount is longer than the string, the remainder of the string will be removed.
Example
The following example demonstrates the use of CStringT::Delete.
//typedef CStringT< TCHAR, StrTraitATL< TCHAR > > CAtlString;
CAtlString str( "Soccer is best!");
printf("Before: %s\n", (LPCTSTR) str);
int n = str.Delete(6, 3);
printf("After: %s\n", (LPCTSTR) str);
_ASSERT(n == str.GetLength());
Output
Before: Soccer is best!
After: Soccer best!
See Also
CStringT Overview | Class Members
"If I don't see you in this world, I'll see you in the next one... and don't be late." ~ Jimi Hendrix
|
|
|
|
|
Hi!
I was thinking of that function but in my case the length of the CString depends on the data and is various so I don’t know exactly the iIndex. And that’s why I can’t use the function delete.
Thanks.
|
|
|
|
|
Here's a list of some basic CString members you can use:
CString is quite a powerful and easy class, you'll be able to use these members in order to get the required result.
Good luck!
AllocSysString Allocates a BSTR from CStringT data.
AnsiToOem Makes an in-place conversion from the ANSI character set to the OEM character set.
AppendFormat Appends formatted data to an existing CStringT object.
Collate Compares two strings (case sensitive, uses locale-specific information).
CollateNoCase Compares two strings (case insensitive, uses locale-specific information).
Compare Compares two strings (case sensitive).
CompareNoCase Compares two strings (case insensitive).
Delete Deletes a character or characters from a string.
Find Finds a character or substring inside a larger string.
FindOneOf Finds the first matching character from a set.
Format Formats the string as sprintf does.
FormatMessage Formats a message string.
FormatMessageV Formats a message string using a variable argument list.
FormatV Formats the string using a variable list of arguments.
GetEnvironmentVariable Sets the string to the value of the specified environment variable.
Insert Inserts a single character or a substring at the given index within the string.
Left Extracts the left part of a string.
LoadString Loads an existing CStringT object from a Windows resource.
MakeLower Converts all the characters in this string to lowercase characters.
MakeReverse Reverses the string.
MakeUpper Converts all the characters in this string to uppercase characters.
Mid Extracts the middle part of a string.
OemToAnsi Makes an in-place conversion from the OEM character set to the ANSI character set.
Remove Removes indicated characters from a string.
Replace Replaces indicated characters with other characters.
ReverseFind Finds a character inside a larger string; starts from the end.
Right Extracts the right part of a string.
SetSysString Sets an existing BSTR object with data from a CStringT object.
SpanExcluding Extracts characters from the string, starting with the first character, that are not in the set of characters identified by pszCharSet.
SpanIncluding Extracts a substring that contains only the characters in a set.
Tokenize Extracts specified tokens in a target string.
Trim Trims all leading and trailing whitespace characters from the string.
TrimLeft Trims leading whitespace characters from the string.
TrimRight Trims trailing whitespace characters from the string.
"If I don't see you in this world, I'll see you in the next one... and don't be late." ~ Jimi Hendrix
|
|
|
|
|
Can anyone tell whether can we convert UTF-8 text file to unicode text file programmatically.
|
|
|
|
|
Why not?
Read in each line and use MultiByteToWideChar on it.
Write out the converted strings to another file.
|
|
|
|
|
Any examplecode how to use that MultiByteToWideChar(...) ?
_____________________________
...and justice for all
APe
|
|
|
|
|
MultiByteToWideChar(
CP_UTF8, // code page - UTF-8 since you know that is your source's code page
MB_PRECOMPOSED, // character-type options
lpMultiByteStr, // pointer to your UTF-8 string read from file
cbMultiByte, // number of bytes in string - length of line read from file
lpWideCharStr, // this is where the data will be converted into the wide-character buffer
cchWideChar // size of the wide character buffer
);
I like to make the wide character buffer at least 1.5 times larger than the source string in case you get several multi-bute characters in your input stream.
|
|
|
|
|
hi ,
i'm using coledatetime class for storing time value.
COleDateTime stTime(/*some time value*/);
CString s=stTime.Format(VAR_TIMEVALUEONLY);
if time value is 12:00:00 AM then value of s is blank.
other wise it is giving proper time values.
what could be problem.
thanks,
prasad
|
|
|
|
|
prasad_som wrote:
what could be problem
It is not actually a problem. It is a feature , as documented in the MSDN[^].
Why not specifically specify the time format your require?
e.g. stTime.Format(_T("%H:%M%:S")); <font color=green>// will give '23:16:23' for example</font>
I Dream of Absolute Zero
|
|
|
|
|
By chance, see if this makes a difference:
CString s = stTime.Format((DWORD) VAR_TIMEVALUEONLY);
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
|
|
|
|
|
i want to create a help file just like windows help for my software, how do i proceed. any examples?
|
|
|
|
|
You'll need to use the Windows Help Compiler (hcw.exe) for this. You'll need to create a Help Project (.hpj) file, which provides information to the help compiler. The MakeHm utility will take the #define statements from the project's resource.h file and make coressponding IDs in a .hm file. This is done by prefacing each of the IDs with an "H".
Search MSDN for these various keywords and the rest will fall into place.
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
|
|
|
|
|
I've tried using critical sections and interlocked functions (implemented as spin locks) in my VC++ 6 projects, believing them to be the more efficient means of providing thread synchronization. But I wonder if there's anything faster / more efficient...
Any ideas?
Thanks!
Nico
|
|
|
|
|
a critical section is an efficient solution for synchronization across threads.
Kuphryn
|
|
|
|
|
what are you trying to do so I can help you
gabby
|
|
|
|
|
I'm trying to block off a chunk of code responsible for sending a request to a server and receiving its immediate reply. I enclosed this chunk between the EnterCriticalSection(mutex) and LeaveCriticalSection(mutex) functions, or something like:
while (InterlockedExchange(&s_spinlock, 1) == 1)
{
Sleep(0);
}
//
// send request and receive reply from the server;
// something like send(request, reply)
//
InterlockedExchange(&s_spinlock, 0);
They seem to do the job. But when more client threads are involved (say 200-300), performance decreases significantly.
Basically, what happens within that chunk is the client sends a request to the server for it to perform a queue function (enqueue/dequeue) on a server side queue. The client sends the message to enqueue with the request as well, and then receives some reply from the server that has something to do with the outcome of the queue function.
|
|
|
|
|
I am suspecting the maount of threads is your problem?
You can try to use thread pools to decrease the number of threads in your system.
The context switching among so many threads will ultimately effect your peerformance more than worrying about the time spent on the thread synchronization objects.
|
|
|
|
|
Hello !
I made a program that connects to a Bluetooth device (a BT dongle is connected on my computer). So, I open the communication like a normal serial port (BT port is COM4 on my computer). The difference between the two is that when I try to write something to the port (using the WriteFile function) when nothing is connected, I don't have any timeout and thus my program hangs. The program doesn't hang when a standard serial port is opened...
Anybody have a solution for this problem ?? Of course, my program cannot hang when the device is not powered...
Thanks
|
|
|
|
|
Hmm..
To simplify things: does the BT stack offer the COM4 port even if the device wasn't connected ? I mean, regardless of the availability of the device, the port can always be opened ?
But, when a device is not connected, the port does open but no timeouts occur ?
One solution would be to use a custom timer. On the first attempt to communicate with the device, open the port, create a custom timer, and send a predefined command to the device, to which you know a response. This could be something like querying the serial number or software version. If your custom timer expires before a response is received, it is safe to assume that there is no device connected, and handle this as "No device available" -situation in the application.
-Antti Keskinen
----------------------------------------------
The definition of impossible is strictly dependant
on what we think is possible.
|
|
|
|