Solved

Some error with datatype

Posted on 2004-09-21
12
576 Views
Last Modified: 2013-12-14
hi all,

i had downloaded the mapirule.cpp from the msdn site to intercept an incoming SMS..

i also downloaded a SendSms codes from : http://www.codeproject.com/ce/SMS_CEMPI.asp.
to send sms.

i also downloaded a string tokeniser from : http://www.codeproject.com/cpp/stringtok.asp

i had edited the codes inside the mapirule.ccp

===start codes edited===
if (wcsstr(pspvEmail->Value.lpszW, L"98494778") != NULL)

      {
            MessageBeep(MB_ICONASTERISK);
            MessageBox(NULL, pspvSubject->Value.lpszW, pspvEmail->Value.lpszW, MB_OK);

            //HANDLE hFile = CreateFile(_T("\\My Documents\\Personal\\jobs.txt"),GENERIC_WRITE, FILE_SHARE_READ,NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

            HANDLE hFile;
            const TCHAR* filename = TEXT("\\My Documents\\Personal\\jobs.txt"); // Name of file to be written
            const TCHAR* file_contents = pspvSubject->Value.lpszW; // String to be written to file
            TCHAR message[255]; // String to be displayed in message box
            //DWORD btw;
            DWORD bytesWritten;

            string show="";
            string in_file="";

            //The Results Vector
            vector<string> oResult;
            //Call Tokeniker
            in_file=file_contents;
            CTokenizer<CIsComma>::Tokenize(oResult, in_file, CIsComma());
            //Display Results
            for(int i=0; i<oResult.size(); i++)
              show+=oResult[i]+"TOK";

MessageBox(NULL,show, TEXT("????"), MB_OK);
            
            // Open existing file or create it if it does not exist
            hFile = CreateFile(filename, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);


            if (hFile == INVALID_HANDLE_VALUE)
            MessageBox(NULL,TEXT("Couldn't create the file!"), TEXT("Create File ERROR"), MB_OK);
            else
            {
wsprintf(message, TEXT("%s"), file_contents);
//btw = wcslen(message)*sizeof(TCHAR);

                  // Write a string to the file
                  if (WriteFile(hFile, message ,wcslen(message)*sizeof(TCHAR) , &bytesWritten, NULL)!=0)
                  {
                  //wsprintf(message, TEXT("Success - String %s written to file %s."), file_contents, filename);
                  wsprintf(message, TEXT("%s"), file_contents);

                  
                  }
                  else
                  {
                  wsprintf(message, TEXT("Error writing to file: %d"), GetLastError());
                  }
            

            // Display the message indicating the result
            MessageBox(NULL, message, TEXT("Result"), MB_OK);

            // Close file handle
            CloseHandle(hFile);

      }
            
            // Delete the message and mark it as handled so it won't show up in Inbox
            hr = DeleteMessage(pMsgStore, pMsg, cbMsg, lpMsg, cbDestFolder, lpDestFolder, pulEventType, pHandled);

            DoSendMessage(_T("hhhh"), (LPCTSTR)pspvEmail->Value.lpszA, _T("Ack:ECS/CQY/B2/PEU03A"));
            //DoSendMessage(_T("hhhh"), _T("97665982"), _T("Ack:ECS/CQY/B2/PEU03A"));
      }
===end of codes edited==


however i am getting some error when i combine e three files together as a common workspace (mapirule.vcw that contains the maipule project files)



below are the error i got..
===start of error===
Compiling...
mapirule.cpp
c:\documents and settings\tp\desktop\projfiles\mapirule(20-9)\tokenizer.h(29) : warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
c:\documents and settings\tp\desktop\projfiles\mapirule(20-9)\mapirule.cpp(634) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'const unsigned short *' (or there is no acceptable conversion)
c:\documents and settings\tp\desktop\projfiles\mapirule(20-9)\mapirule.cpp(640) : error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const unsign
===end of error ===

any help will be greatly appreciated..
i need some help urgently..
0
Comment
Question by:TPoly
  • 6
  • 5
12 Comments
 
LVL 19

Assisted Solution

by:drichards
drichards earned 480 total points
Comment Utility
You appear to be doing a unicode buld, so these lines give you trouble:

          in_file=file_contents;
and
          MessageBox(NULL,show, TEXT("????"), MB_OK);

The clues are that it is using MessageBoxW (see error text for line 640) and thinks the string constant is a 'const unsigned short *' instead of a 'const char *'.

You can solve this one of two ways.

1) Turn off UNICODE.  Somewhere, either in a header or more likely in the project properties, you have defined _UNICODE.  You should remove that.

2) use std::wstring instead of std::string.

In either case, there is another problem with the MessageBox.  std::string coes not provide a cast operator to 'const char*', so you need to use:

          MessageBox(NULL,show.c_str(), TEXT("????"), MB_OK);

This will work with either change 1 or 2.
0
 
LVL 19

Assisted Solution

by:drichards
drichards earned 480 total points
Comment Utility
Just to be clear, use method 1 or 2, but not both, or you will end up with the opposite problem of the one you currently have.
0
 
LVL 19

Assisted Solution

by:drichards
drichards earned 480 total points
Comment Utility
Also, the warning is that you have a line somewhere that is trying to cast an int to a bool.  This is usually solved by doing something like:

   bool result = (val != testVal);

or

   bool result = (val == testVal);

where val is an integer and testVal is the test value.  The condition should be made appropriate (might be <, >, etc.).
0
 

Author Comment

by:TPoly
Comment Utility
Thanks for the speedy reply, drichards.

I've edited my code using method 2.
But there are still some problems.

The boolean warning is solved.

=========edited code=========
                               const TCHAR* filename = TEXT("\\My Documents\\Personal\\jobs.txt"); // Name of file to be written
            const TCHAR* file_contents = pspvSubject->Value.lpszW; // String to be written to file
            TCHAR message[255]; // String to be displayed in message box
            //DWORD btw;
            DWORD bytesWritten;

            wstring show="";
            wstring in_file="";

            //The Results Vector
            vector<string> oResult;
            //Call Tokeniker
            in_file=file_contents;
            CTokenizer<CIsComma>::Tokenize(oResult, in_file, CIsComma());
            //Display Results
            for(int i=0; i<oResult.size(); i++)
              show+=oResult[i]+"TOK";

                               MessageBox(NULL,show.c_str(), TEXT("????"), MB_OK);
=========edited code=========

++++++++++errors++++++++++++
Compiling...
mapirule.cpp


c:\documents and settings\tp\desktop\projfiles\mapirule(20-9)\mapirule.cpp(628) : error C2440: 'initializing' : cannot convert from 'char [1]' to 'class std::basic_string<unsigned short,struct std::char_traits<unsigned short>,class std::allocator<unsigned short> >'

No constructor could take the source type, or constructor overload resolution was ambiguous


c:\documents and settings\tp\desktop\projfiles\mapirule(20-9)\mapirule.cpp(629) : error C2440: 'initializing' : cannot convert from 'char [1]' to 'class std::basic_string<unsigned short,struct std::char_traits<unsigned short>,class std::allocator<unsigned short> >'

No constructor could take the source type, or constructor overload resolution was ambiguous


c:\documents and settings\tp\desktop\projfiles\mapirule(20-9)\mapirule.cpp(635) : error C2664: 'Tokenize' : cannot convert parameter 2 from 'class std::basic_string<unsigned short,struct std::char_traits<unsigned short>,class std::allocator<unsigned short> >' to 'const class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &'
       
Reason: cannot convert from 'class std::basic_string<unsigned short,struct std::char_traits<unsigned short>,class std::allocator<unsigned short> >' to 'const class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >'
No constructor could take the source type, or constructor overload resolution was ambiguous


c:\documents and settings\tp\desktop\projfiles\mapirule(20-9)\mapirule.cpp(638) : error C2679: binary '+=' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable conversion)


Error executing clarm.exe.


mapirule.obj - 4 error(s), 0 warning(s)
++++++++++errors++++++++++++
0
 
LVL 19

Assisted Solution

by:drichards
drichards earned 480 total points
Comment Utility
Looks like you'll have to turn off UNICODE.  The wstring problems can be easily fixed:

          wstring show=L"";
          wstring in_file=L"";

and the vector needs to be 'vector<wstring>'.

Unfortunately, I don't know what CTokenizer is and it seems to want only string parameters.  You need to recode the functionality of CTokenizer or change the build so it doesn't use UNICODE and put all the wstring's back to string's.
0
 
LVL 19

Expert Comment

by:drichards
Comment Utility
OK, I looked up CTokenizer on CodeProject.  It does not handle wstring's, so your only option is to go back to string's and get rid of the _UNICODE.  Otherwise, you have to dump or rewrite CTokenizer.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:TPoly
Comment Utility
Actually I need to use unicode.

Do you know of any other tokenizer that handles wstrings?
0
 
LVL 19

Accepted Solution

by:
drichards earned 480 total points
Comment Utility
I don't know of one, but here's one I wrote.  It's cheap, but it works.

class CTokenizerW
{
public:
    static Tokenize(std::vector<std::wstring> &result, std::wstring &input, wchar_t delim)
    {
        std::wstring::size_type len = input.length();
        std::wstring::size_type offset = 0;
        while (offset <= len)
        {
            std::wstring::size_type delimPos = input.find(delim, offset);
            result.push_back(std::wstring(input.substr(offset, delimPos-offset)));
            offset = delimPos < len ? delimPos+1 : delimPos;
        }
    }
};

Your code would looklike this:


          wstring show=L"";
          wstring in_file=L"";

          //The Results Vector
          vector<wstring> oResult;
          //Call Tokeniker
          in_file=file_contents;
          CTokenizerW::Tokenize(oResult, in_file, (wchar_t)',');
          //Display Results
          for(int i=0; i<oResult.size(); i++)
            show+=oResult[i]+L"TOK";
0
 

Author Comment

by:TPoly
Comment Utility
so your CTokenizerW class change with the Tokenizer.h 's class CTokenizer is it?

and
======
Your code would looklike this:


          wstring show=L"";
          wstring in_file=L"";

          //The Results Vector
          vector<wstring> oResult;
          //Call Tokeniker
          in_file=file_contents;
          CTokenizerW::Tokenize(oResult, in_file, (wchar_t)',');
          //Display Results
          for(int i=0; i<oResult.size(); i++)
            show+=oResult[i]+L"TOK";
=========

jus edit in the mapirule.cpp?
0
 

Author Comment

by:TPoly
Comment Utility
To add to my last post:

I am using eMbedded Visual C++ for this project, does it matter?
0
 

Author Comment

by:TPoly
Comment Utility
==new error===

Compiling...
mapirule.cpp
c:\program files\windows ce tools\wce420\pocket pc 2003\include\armv4\xstring(724) : warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify -GX
        c:\program files\windows ce tools\wce420\pocket pc 2003\include\armv4\xstring(720) : while compiling class-template member function 'void __cdecl std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::_Copy(unsign
ed int)'
c:\program files\windows ce tools\wce420\pocket pc 2003\include\armv4\xstring(724) : warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify -GX
        c:\program files\windows ce tools\wce420\pocket pc 2003\include\armv4\xstring(720) : while compiling class-template member function 'void __cdecl std::basic_string<unsigned short,struct std::char_traits<unsigned short>,class std::allocator<u
nsigned short> >::_Copy(unsigned int)'

mapirule.obj - 0 error(s), 2 warning(s)

===end of error===
0
 
LVL 17

Assisted Solution

by:rstaveley
rstaveley earned 20 total points
Comment Utility
Add -GX to your compiler options. Stack unwinding isn't enabled by default, but you need it with STL.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now