dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: FindWindow(NULL,"windowname") using a string variable instead of "windowname"

  1. #1
    Join Date
    Nov 2008
    Posts
    8

    FindWindow(NULL,"windowname") using a string variable instead of "windowname"

    As the title suggests I am working on a project and
    FindWindow(NULL,"windowname") works fine, but for my project, I need to pull the windowname from a string variable, for example String* Hereitis = "temp.pdf - Adobe Reader". My thought was FindWindow(NULL, Hereitis) would work but instead I get the error can't convert second variable from string to lpcstr. I am using .net visual C++. Here is the section of applicable code

    String* view = "c:\\scan\\program\\pdfviewer.txt";
    FileStream* fsview = new FileStream(view, FileMode::Open);
    StreamReader* srview = new StreamReader(fsview);
    String* viewpath = "Start";
    viewpath = srview->ReadLine();
    fsview->Close();
    String* closepath = String::Concat("temp.pdf - ",viewpath);
    HWND closer = FindWindow(NULL, closepath);
    SendMessage(closer,WM_CLOSE,0,0);

  2. #2
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,019

    Re: FindWindow(NULL,"windowname") using a string variable instead of "windowname"

    Try the (LPCTSTR) or (LPCSTR) operator
    Code:
    HWND closer = FindWindow(NULL, (LPCSTR) closepath);
    Succinct is verbose for terse

  3. #3
    Join Date
    Nov 2008
    Posts
    8

    Re: FindWindow(NULL,"windowname") using a string variable instead of "windowname"

    I tried both of those and get the same error c2440: 'type cast' : cannot convert from 'System::String_gc* to 'LPCTSTR' or 'LPCSTR' for each.

  4. #4
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: FindWindow(NULL,"windowname") using a string variable instead of "windowname"

    Are you aware that you're mixing managed (.NET) and native C++ code here ?

    If you don't know the difference STOP AND FIND OUT. Otherwise you'll get yourself into heaps of problems.

    You need to convert the .NET string (System.String) to a native C++ char array to pass to FindWindow.

    See here.

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  5. #5
    Join Date
    Nov 2008
    Posts
    8

    Re: FindWindow(NULL,"windowname") using a string variable instead of "windowname"

    Thanks that did the trick, where or what do you recommend reading to know the difference between the two types.

  6. #6
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: FindWindow(NULL,"windowname") using a string variable instead of "windowname"

    Get a book on learning native C++ (it's easy to spot - it'll be full of 'std::') and read that.

    Then come back to .NET code and see the difference.

    Out of interest why aren't you learning C# if you're interested in learning .NET and don't know native C++ ?

    Then there's no confusion.

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  7. #7
    Join Date
    Nov 2008
    Posts
    8

    Re: FindWindow(NULL,"windowname") using a string variable instead of "windowname"

    I got a deluxe learning edition a while back of visual c++ .net version 2003 and so have been using it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)