Hottest Forum Q&A on CodeGuru - January 19th - 2004

Introduction:

Lots of hot topics are covered in the Discussion Forums on CodeGuru. If you missed the forums this week, you missed some interesting ways to solve a problem. Some of the hot topics this week include:


Why do get I compiler error C2065 when compiling my VC++ 6.0 project in VC7? (top)

Thread:

tiff, a junior member, converted his project from VC++ 6.0 to VC7 and got compiler error 2065. He accepted all default conversions from VC7, but it still does not work. What can be done here?

When I convert a Visual C++ 6.0 project to Visual C++ .NET by
opening the project using .NET environment, and accepting all VC 7.0
conversion defaults, the new project would not compile, and gives
this error:
Compiler error C2065: '_mbstowcsz': undeclared identifier.
This functions compiles ok on VC++ 6.0.

Because VC++6.0 and VC++.NET put this function in different
locations, I did change the included path, and made it use Dynamic
ATL because in VC++.NET this function is in atlmfc/include. But
nothing works.

So, guys, do you know why this does not work? This question is again for you. Can you answer it? If yes, e-mail me and I will publish the answer in next week's column.


Should I call destroywindow before deleting an object? (top)

Thread:

brraj is curious whether he should call DestroyWindow() prior deleting the object. Do you know whether this is correct or not?

I have a CEdit *m_pEdit pointer; it will be dynamically created
by using the new and create functions and deleted by using delete.
My question is before deleting should we call the DestroyWindow
function? For example:
m_pEdit->DestroyWindow();
delete m_pEdit;
m_pEdit = 0;
Is the destroywindow necessary?
My senior says that window will not be destroyed if I don't call
DestroyWindow.

Well, your senior is correct. You will need to call DestroyWindow() before calling delete. Any class derived from CWnd will call DestroyWindow from its destructor. But remember, if you derive your own class from a CWnd-derived class, and you override the function, you will need to call DestroyWindow explicitly in your own code.


Where is the error log? (top)

Thread:

paraglidersd is working on a project that is nearly finished. But, he still gets an error in which he needs the error log on the system. Unfortunately, he is not able to find the log. Do you know where such a log file is located?

I am running an Visual C++ application on a laptop that does not 
have Visual Studio installed. I (obviously) created the application
on a different machine. This application is a simple Win32
application with no interactive windows. It merely runs (after
double-clicking it) and waits for information to come across a
serial port. It has some threading.

The error I am receiving is "Application-so-and-so has generated
errors and will be closed by Windows. You will need to restart the
program. An error log is being created."

I cannot find the error log anywhere on my system. I dont even know
what the name of the log file would be. I couldn't find anything on
MSDN. Help? I am on a deadline and am pulling my hair out.

The error log could be the Dr. Watson Log. Enter DRWTSN32 in START/RUN. Also, check that you have the correct version of the DLLs on your laptop. These are the DLLs required on the laptop:

  • MSVCP60.DLL
  • PSAPI.DLL

An another option is to debug your application remotely.

After suggesting all that, paraglidersd found the solution on his own. The program used a logging function that inherited from another code. The error was in the logic of the code.


How do I search for keys using wildcards in a map? (top)

Thread:

Aks82 is working with maps where he needs to search for wildcards. Is it possible?

I am planning to create a map for my given data set. I was
wondering whether there is a way to search for keys by using regular
expressions. Basically, if the user forgets the key, can s/he search
for the key using wildcards?

Fr example, if HostID is one such key, is there a way I can search
for all keys that begin with 'Host' as 'Host*' or something?

I know that the find() method allows one to search for the key as an
exact expression. But my tool needs to be able to search for the
'keys' using regular expressions.

Unfortunately, there isn't any function to search for wildcards. But, to get the desired result, you can use the std::lower_bound() function. Here is how it might look:

#include <iostream>
#include <map>
#include <string>

using namespace std;

typedef std::pair<string,int> PAIR;

int main()
{
    map<string,int> my_map;
    
    my_map.insert( PAIR("Server1",1) );
    my_map.insert( PAIR("Host3",3) );
    my_map.insert( PAIR("Server2",2) );
    my_map.insert( PAIR("Host1",1) );
    my_map.insert( PAIR("Something Else",6) );
    my_map.insert( PAIR("Host2",2) );
    my_map.insert( PAIR("guest",44) );
    my_map.insert( PAIR("HostID",32) );

    // find all entries that start with "Host"

    string str_to_find = "Host";
    int nLength = str_to_find.length();

    map<string,int>::iterator it = my_map.lower_bound("Host");

    while (it != my_map.end())
    {
        if (it->first.substr(0,nLength) != str_to_find) break;

        cout << it->first << " " << ;it->second << endl;
        ++it;
    }

    return 0;
}


How can I use a *void -> *short in printf? (top)

Thread:

yiannakop asked a very interesting question.

Hi everyone. Suppose I have the following code:
void *var;
int c;       // c given by user...
...
...
switch (c)
{
  case 1:
     var = (short*)malloc(sizeof(short));
     scanf("%d",(short*)var);
     printf("content of var: %d\n",*(short*)var);
     break;
  case 2:    // same for float
    ...
    ...
    ...
}
The above program works fine with all types (int, float, double),
but not for short. I suppose %d for shorts is not right under
Solaris 5.7?

The following code should work:

scanf("%hd",(short*)var);

Microsoft says that %h is a MS-specific extension. Orginally quote from MSDN:

"The optional prefixes to type, h, l, and L, specify the .size. of argument (long or short, single-byte character or wide character, depending upon the type specifier that they modify). These type-specifier prefixes are used with type characters in printf functions or wprintf functions to specify interpretation of arguments, as shown in the following table. These prefixes are Microsoft extensions and are not ANSI-compatible."

But, the C99 standard also contains this extension. Take a look at the whole thread to learn more about this topic.




About the Author

Sonu Kapoor

Sonu Kapoor is an ASP.NET MVP and MCAD. He is the owner of the popular .net website http://dotnetslackers.com. DotNetSlackers publishs the latest .net news and articles - it contains forums and blogs as well. His blog can be seen at: http://dotnetslackers.com/community/blogs/sonukapoor/

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • Hundreds of millions of users have adopted public cloud storage solutions to satisfy their Private Online File Sharing and Collaboration (OFS) needs. With new headlines on cloud privacy issues appearing almost daily, the need to explore private alternatives has never been stronger. Join ESG Senior Analyst Terri McClure and Connected Data in this on-demand webinar to take a look at the business drivers behind OFS adoption, how organizations can benefit from on-premise deployments, and emerging private OFS …

Most Popular Programming Stories

More for Developers

RSS Feeds