Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. string class help needed

string class help needed

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionc++
7 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CHYGO
    wrote on last edited by
    #1

    i ues string class in a Win32 Console Application(build by VC++ 6),here is the code and errors, how can i fix it?

    #include "stdafx.h"

    //add
    #include <windows.h>
    #include <stdio.h>
    #include <tchar.h>
    #include "Psapi.h"
    #include <iostream.h>
    #include <string>
    using namespace std;

    void PrintProcessNameAndID( DWORD processID )
    {
    string szProcessName;

    // Get a handle to the process.
    
    HANDLE hProcess = OpenProcess( PROCESS\_QUERY\_INFORMATION |
                                   PROCESS\_VM\_READ,
                                   FALSE, processID );
    
    // Get the process name.
    
    if (NULL != hProcess )
    {
        HMODULE hMod;
        DWORD cbNeeded;
    
        if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), 
             &cbNeeded) )
        {
    		int len = lstrlen(szProcessName);
            GetModuleBaseName( hProcess, hMod, szProcessName, 
                               len);
    		//theProName = szProcessName;
        }
    
    if(lstrcmp(szProcessName, "smss.exe"))
    	\_tprintf( TEXT("%s  (PID: %u)\\n"), szProcessName, processID );
    }
    
    CloseHandle( hProcess );
    

    }

    int main(int argc, char* argv[])
    {
    ...
    return 0;
    }

    errors: ... : error C2664: 'lstrlenA' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called ... : error C2664: 'GetModuleBaseNameA' : cannot convert parameter 3 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'char *' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called ... : error C2664: 'lstrcmpA' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

    i 've tried ,then i have no regret

    N 1 Reply Last reply
    0
    • C CHYGO

      i ues string class in a Win32 Console Application(build by VC++ 6),here is the code and errors, how can i fix it?

      #include "stdafx.h"

      //add
      #include <windows.h>
      #include <stdio.h>
      #include <tchar.h>
      #include "Psapi.h"
      #include <iostream.h>
      #include <string>
      using namespace std;

      void PrintProcessNameAndID( DWORD processID )
      {
      string szProcessName;

      // Get a handle to the process.
      
      HANDLE hProcess = OpenProcess( PROCESS\_QUERY\_INFORMATION |
                                     PROCESS\_VM\_READ,
                                     FALSE, processID );
      
      // Get the process name.
      
      if (NULL != hProcess )
      {
          HMODULE hMod;
          DWORD cbNeeded;
      
          if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), 
               &cbNeeded) )
          {
      		int len = lstrlen(szProcessName);
              GetModuleBaseName( hProcess, hMod, szProcessName, 
                                 len);
      		//theProName = szProcessName;
          }
      
      if(lstrcmp(szProcessName, "smss.exe"))
      	\_tprintf( TEXT("%s  (PID: %u)\\n"), szProcessName, processID );
      }
      
      CloseHandle( hProcess );
      

      }

      int main(int argc, char* argv[])
      {
      ...
      return 0;
      }

      errors: ... : error C2664: 'lstrlenA' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called ... : error C2664: 'GetModuleBaseNameA' : cannot convert parameter 3 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'char *' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called ... : error C2664: 'lstrcmpA' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

      i 've tried ,then i have no regret

      N Offline
      N Offline
      Naveen
      wrote on last edited by
      #2

      Trying using szProcessName.c_str() instead of only "szProcessName" in the functions 'lstrlenA' , 'GetModuleBaseNameA' and 'lstrcmpA'.

      nave [OpenedFileFinder] [My Blog]

      C 1 Reply Last reply
      0
      • N Naveen

        Trying using szProcessName.c_str() instead of only "szProcessName" in the functions 'lstrlenA' , 'GetModuleBaseNameA' and 'lstrcmpA'.

        nave [OpenedFileFinder] [My Blog]

        C Offline
        C Offline
        CHYGO
        wrote on last edited by
        #3

        Thank u,Naveen.I did as u told me ,but comes a new error: ...:error C2664: 'GetModuleBaseNameA' : cannot convert parameter 3 from 'const char *' to 'char *' how to do?

        i 've tried ,then i have no regret

        C N 2 Replies Last reply
        0
        • C CHYGO

          Thank u,Naveen.I did as u told me ,but comes a new error: ...:error C2664: 'GetModuleBaseNameA' : cannot convert parameter 3 from 'const char *' to 'char *' how to do?

          i 've tried ,then i have no regret

          C Offline
          C Offline
          CHYGO
          wrote on last edited by
          #4

          oh yea.. haha i must be looking like a fool now... i fix it thanks for yr help :)

          i 've tried ,then i have no regret

          1 Reply Last reply
          0
          • C CHYGO

            Thank u,Naveen.I did as u told me ,but comes a new error: ...:error C2664: 'GetModuleBaseNameA' : cannot convert parameter 3 from 'const char *' to 'char *' how to do?

            i 've tried ,then i have no regret

            N Offline
            N Offline
            Naveen
            wrote on last edited by
            #5

            hmm.. I modified you code a little.Just changed the szProcessName to TCHAR.

            void PrintProcessNameAndID( DWORD processID )
            {
            TCHAR szProcessName[MAX_PATH];
            // Get a handle to the process.
            HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
            PROCESS_VM_READ,
            FALSE, processID );
            // Get the process name.
            if (NULL != hProcess )
            {
            HMODULE hMod;
            DWORD cbNeeded;

                if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), 
                     &cbNeeded) )
                {
                    GetModuleBaseName( hProcess, hMod, szProcessName, 
                                                               MAX\_PATH);
                }
                if(lstrcmp(szProcessName, "smss.exe"))
            	\_tprintf( TEXT("%s  (PID: %u)\\n"), szProcessName, processID );
            }
            
            CloseHandle( hProcess );
            

            }

            nave [OpenedFileFinder] [My Blog]

            C 1 Reply Last reply
            0
            • N Naveen

              hmm.. I modified you code a little.Just changed the szProcessName to TCHAR.

              void PrintProcessNameAndID( DWORD processID )
              {
              TCHAR szProcessName[MAX_PATH];
              // Get a handle to the process.
              HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
              PROCESS_VM_READ,
              FALSE, processID );
              // Get the process name.
              if (NULL != hProcess )
              {
              HMODULE hMod;
              DWORD cbNeeded;

                  if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), 
                       &cbNeeded) )
                  {
                      GetModuleBaseName( hProcess, hMod, szProcessName, 
                                                                 MAX\_PATH);
                  }
                  if(lstrcmp(szProcessName, "smss.exe"))
              	\_tprintf( TEXT("%s  (PID: %u)\\n"), szProcessName, processID );
              }
              
              CloseHandle( hProcess );
              

              }

              nave [OpenedFileFinder] [My Blog]

              C Offline
              C Offline
              CHYGO
              wrote on last edited by
              #6

              thanks for ue help nave, but yr code still have a little problem:)

              if(lstrcmp(szProcessName, "smss.exe"))
              _tprintf( TEXT("%s (PID: %u)\n"), szProcessName, processID );

              that is: function lstrcmp(lpstring1, lpstring2),when lpstring1 equals lpstring2,the return value is zero.so if i need only see "smss.exe" on the screen,i should change it like this

              if(!lstrcmp(szProcessName, "smss.exe"))
              _tprintf( TEXT("%s (PID: %u)\n"), szProcessName, processID );

              i 've tried ,then i have no regret

              D 1 Reply Last reply
              0
              • C CHYGO

                thanks for ue help nave, but yr code still have a little problem:)

                if(lstrcmp(szProcessName, "smss.exe"))
                _tprintf( TEXT("%s (PID: %u)\n"), szProcessName, processID );

                that is: function lstrcmp(lpstring1, lpstring2),when lpstring1 equals lpstring2,the return value is zero.so if i need only see "smss.exe" on the screen,i should change it like this

                if(!lstrcmp(szProcessName, "smss.exe"))
                _tprintf( TEXT("%s (PID: %u)\n"), szProcessName, processID );

                i 've tried ,then i have no regret

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                CHYGO wrote:

                if(!lstrcmp(szProcessName, "smss.exe"))

                lstrcmp() is not a boolean function so you should refrain from treating it as such:

                if (lstrcmp(szProcessName.c_str(), "smss.exe") == 0)
                ...

                Also, since szProcessName is a string variable, why not just use:

                if (szProcessName != "smss.exe")
                ...

                "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups