|
Anonymous wrote:
<br />
CString szMan; <br />
const char* chxx = xx;<br />
szMan.Format("%s", chxx);<br />
That code is attempting to cast a BSTR/_bstr_t to a const char * , which is a no-no.
And, if all you need to so is display the contents of the BSTR as an ASCIIZ string, you can change %s to a %S (uppercase) and it will treat the parameter as a wide string, which is what a BSTR contains:
<br />
szMan.Format( _T( "%S" ), x );
Peace!
-=- James.
"Some People Know How To Drive, Others Just Know How To Operate A Car."
[Get Check Favorites 1.4 Now!]
|
|
|
|
|
This solution suffer from the same problem than the original code that you try to correct...
Conversions operators are not called for function which variables arguments and the whole object is push on the stack.
_bstr_t is not a BSTR. It's an object that contains a pointer to a sub-object that will hold the BSTR and the converted string (if it was asked).
Philippe Mori
|
|
|
|
|
> This solution suffer from the same problem than the original
> code that you try to correct...
If you are referring to my typo, then yes. If you are saying that I cannot use a BSTR like that, then no.
> Conversions operators are not called for function which variables
> arguments and the whole object is push on the stack
That is why the format specifiers tell _stprintf/CString::Format(...) how to treat the parameter:
>> szMan.Format( _T( "%S" ), x );
Under an ANSI build, the "%S" (remember: UPPERCASE) tells CString::Format(...) to treat the passed parameter as a WIDE STRING, which is what x (a BSTR ) really is: A BSTR is a specially allocated _wchar_t string with a length value (a DWORD ) 4-bytes before the pointer value.
Peace!
-=- James.
"Some People Know How To Drive, Others Just Know How To Operate A Car."
[Get Check Favorites 1.4 Now!]
|
|
|
|
|
Hmmm... I am under the impression that when the temporary bstr_t object is destroyed, it deallocates the BSTR it wrapped...? That would cause the BSTR you return to be invalid. Try looking at the contents (not value) of *pRetval in the debugger right when you get to the delete [] sz line.
You should just be using SysAllocString(...) directly, similar to:
<br />
#include <AFXPriv.h>
<br />
USES_CONVERSION;<br />
<br />
*pRetval = ::SysAllocString( A2CW( sz ) );<br />
Peace!
-=- James.
"Some People Know How To Drive, Others Just Know How To Operate A Car."
[Get Check Favorites 1.4 Now!]
|
|
|
|
|
No! The BSTR object is valid. _bstr_t in this case is just attaching itself to the object without handling the memory allocation/deallocation process.
Thanx to both of you for the efforts and time spent.
rechi
|
|
|
|
|
Be VERY CAREFUL here (look into what I say before answering): (Assuming VC++ 6.0 here) When your code assigns the _bstr_t object to the dreferenced BSTR pointer, all you are doing is calling _bstr_t::operator wchar_t*(...) , NOT a BSTR -specific operator, which would correctly COPY the BSTR . When the temporary object goes out of scope, the contained BSTR gets SysFreeString(...) -ed, so you have just returned an already-freed BSTR to the caller! Sooner or later, code like that is gonna break down.
Again: do not use a shotgun to kill a fly: correctly use SysAllocString(...) directly to allocate the BSTR that is going to be returned to the caller.
Peace!
-=- James.
"Some People Know How To Drive, Others Just Know How To Operate A Car."
[Get Check Favorites 1.4 Now!]
|
|
|
|
|
I have a COM object that starts Microsoft Word and opens a word document. The user can make any modifications to the docunment. I added a special button that is going to be used to save the document and exit Microsoft Word. Unfortunately, I am not sure how to capture the event to know the button has been hit. I have looked through the posts and articles and there is a lot of information if you are an ATL server. But this is an MFC-based COM object and is not an ATL server. Does anyone know what do I need to do, in plain english, to determine when my button has been hit. Am I going to have to make it an ATL server to get the event and if so, how do I do that?
PLEASE HELP!!!!!
|
|
|
|
|
you've asked this question at least three times in the past 24 hours. if you haven't received an answer, it's probably because nobody here knows.
-c
Cheap oil. It's worth it!
|
|
|
|
|
Way to go dude, I must admit you have to be the biggest jackass I have noticed on this messageboard. If you don't have a reply that is helpful, don't leave any reply at all!
|
|
|
|
|
you didn't really give us much to work with so, what do you expect?
but:
did you try Q183599 in the MSDN "HOWTO: Catch Microsoft Word97 Application Events Using VC++" ?
did you look at: "Automation And Eventing With Microsoft Word" http://www.codeguru.com/atl/wordauto.shtml ?
did you look at: http://www.codeproject.com/com/word_ole_bm.asp ?
how about : http://msdn.microsoft.com/library/en-us/dnguion/html/drgui102599.asp ?
Cheap oil. It's worth it!
|
|
|
|
|
Yes, I have looked at all of these examples and they do not necessarily match the scenario I have now. I am using Automation to call MS Word, next I open a document, then I dynamically add a button to the Word toolbar using the commandbars functions. But none of the above examples will work, since they all use ATL and my application does not. All I am looking for is some knowledge as to how I can capture the event that the button that I dynamically created, not one that is on a dialog box or so forth, was pushed. If it was going to cause this much controversy, I would not have put my post out here. I noticed another person's post, Al_ something or other, having a similar problems. So it isn't just me having problems...
By the way, thank you for the advice but it didn't help...
|
|
|
|
|
I'm not sure about this, but I may have an idea that may flukily do what you need (though I'm not sure how efficient or how well it conforms to com standards - it is just an idea). Could you not use a hook procedure to grab 'control' of the apps messages and test to see when your button has been pressed by the message that is sent to the app? Not sure about whether this is even capable, just an idea I thought I might share.
Alan.
"When I left you I was but the learner, now I am the master" - Darth Vader
|
|
|
|
|
I need to create a wizard to gather information from the userregarding a file.
Currently, I want to use the wizard in two different ways (this may increase in the future)
1. As a standalone wizard
2. As part of a File Open/Save dialog
My dilemma is how to create the wizard.
Should I :
a. Create as a class which is then included in each project where the wizard is required
b. Create as an ATL control which can then be used in any container in the future - even though the wizard is not a simple control e.g. it needs to contain a combo box, edit controls etc. and talk to a database.
c. Create in some other way.
Anyone suggestions are welcome...
Thanks
Nic
|
|
|
|
|
I'd go for a, which is way simpler than other options. This is how most controls, wizards and other stuff are provided here at CP.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
In general it's probably a matter of preference. I would personally create a Static library with the Visual Studio Wizard and put your wizard/DB code in there. Then I would just include that library in whatever project I was using. The reason I choose this method is because I like to statically link things that I have control over. That is just my personal preference, though.
If you have questions about doing what I've suggested, let me know and I'll walk you through it. BTW, did you know that property sheets have a wizard mode? Would accomplish what you are wanting to do pretty easily. Something you may want to look into.
-Matt
------------------------------------------
The 3 great virtues of a programmer:
Laziness, Impatience, and Hubris.
--Larry Wall
|
|
|
|
|
Whilst b is a nice option, it means a lot of extra work without much extra gain (unless a third party app is going to call your wizard).
I'd stick with a, you can always wrap it in a COM object later should the need arise.
Michael
Logic, my dear Zoe, merely enables one to be wrong with authority. - The Doctor
|
|
|
|
|
Thanks guys, I knew I should have stuck with my first instinct and you have confirmed that for me !!!!!!!!!
Nic
|
|
|
|
|
Hi all...
i'm trying to list the avalible trays for specifice printer. for that list i use combo box so the user can choose what tray he/she wants the printer to use to print.
so i search and only find setting landscape, paper before the printing.
but i found out about
Devicecapabilities(...) to get avalible tray (Word) array. then i can set the tray by using DEVMODE.dmDefaultSource .
so i code like this
<br />
void CSMPprojectDlg::ListTrays(CWordArray *traysArray)<br />
{<br />
int arrayLength = traysArray->GetSize();<br />
<br />
for(int i=0;i< arrayLength; i++)<br />
{<br />
WORD tray = traysArray->GetAt(i);<br />
m_FolderTrays.InsertString(i,(LPCTSTR)tray);<br />
m_FileTrays.InsertString(i,(LPCTSTR)tray);<br />
m_ErrorTrays.InsertString(i,(LPCTSTR)tray);<br />
}<br />
UpdateData(TRUE);<br />
}<br />
<br />
void CSMPprojectDlg::SetDefaultPrinter()<br />
{<br />
CPrintDialog dlg(FALSE);<br />
dlg.GetDefaults();<br />
defaultPrinter = dlg.GetPrinterDC();<br />
dcPrinter.Attach(defaultPrinter);<br />
<br />
if(theApp.my_DevMode !=NULL)<br />
::GlobalFree(theApp.my_DevMode);<br />
<br />
theApp.my_DevMode = dlg.m_pd.hDevMode;<br />
lp = (LPDEVMODE) ::GlobalLock(theApp.my_DevMode);<br />
ASSERT(lp);<br />
<br />
CString pDevice= dlg.GetDeviceName();
CString pPort= dlg.GetPortName();
LPTSTR pOutput;
if(DeviceCapabilities(pDevice,pPort,DC_BINS, pOutput,lp) != -1)<br />
{<br />
if(pOutput != 0)<br />
{<br />
ListTrays((CWordArray*)pOutput);
}<br />
}<br />
<br />
}<br />
does anyone of you have same problem?
can u give me sample code or link about it.
thanks
|
|
|
|
|
I have a file opened with ifstream and I need to get to the top of the file. Right now I close the file, then open it back up again but thats pretty inefficient. For some reason I remember doing file.seekg(0); at school, but that doesnt seem to work.
-Raffi
|
|
|
|
|
You might wanna do a file.clear() to clear the state. E.g the eof bit might have been set earlier.
|
|
|
|
|
Perfect, thanks
-Raffi Yet another example of my bad programming skills.
|
|
|
|
|
I am currently working on a system that uses an Access database to store a list of url's waiting to be processed. I need to be able to add to, and amend the database, and so, I have derived a class from the CRecordset class called CURLRecordset using the classwizard.
In my application, I can succesfully open a CDatabase object in order to connect to the database, but when it comes to opening the actual recordsets associated with it:
/*
CURLRecordset* unparsed_urls;
CDatabase* db;
CString sql;
*/
unparsed_urls = new ( CUnparsed ) ( db );
sql.Format ( "SELECT * FROM urls WHERE status = %d;", STATUS_NOT_CRAWLED );
unparsed_urls->Open ( CRecordset::snapshot, (LPCTSTR)sql );
I get the following exception:
"Invalid character value for cast specification on column number 2 (urladdress)"
I have traced the source of the exception to the final move() function call in the CRecordset class's Open() method, but I have no idea how to deal with it. My database schema is as follows:
TABLE urls =
(
long urlid (PRIMARY KEY)
VARCHAR(256) urladdress
long status
long level
long keywordid
long sourceid
)
The header file for the CURLRecordset class is as follows:
class CURLRecordset : public CRecordset
{
public:
CURLRecordset(CDatabase* pDatabase = NULL);
DECLARE_DYNAMIC(CURLRecordset)
// Field/Param Data
//{{AFX_FIELD(CURLRecordset, CRecordset)
long m_keywordid;
long m_level;
long m_sourceid;
long m_status;
CString m_urladdress;
long m_urlid;
//}}AFX_FIELD
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CURLRecordset)
public:
virtual CString GetDefaultConnect(); // Default connection string
virtual CString GetDefaultSQL(); // Default SQL for Recordset
virtual void DoFieldExchange(CFieldExchange* pFX); // RFX support
//}}AFX_VIRTUAL
// Implementation
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
};
Any help will be greatly appreciated!
|
|
|
|
|
I think the problem may be in your DoFieldExchange() function...I can't think of anywhere else that the problem could be.
|
|
|
|
|
Hi guys, there's a nice article here on CP with a C++ implementation of the Blowfish algorithm for encrypting files. The examples given however are a bit simplistic and I can't seem to get it to encrypt a text file. Has anyone done that yet?
Senkwe
Just another wannabe code junky
|
|
|
|
|
0. initialize the cipher
1. open input file
2. open output file (binary mode)
3. while (you haven't read the whole input file)
3a. get a block of text from the input
3b. encrypt it
3c. write it
4. close both
the last block will probably require padding to the cipher's block size
-c
Cheap oil. It's worth it!
|
|
|
|