Thread: Workaround needed for sprintf in Windows 7

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    10

    Workaround needed for sprintf in Windows 7

    Hello,

    I am working on a program for instrumentation hardware using Visual Studio C++ on the .NET 2003 framework. The code would work fine on XP, but we're trying to get everything on Windows 7. Almost all the conversion problems have been handled, but the "sprintf" is still have its problems. They compile fine, but when I run tests using the hardware, it throws up a buffer overflow exception. Is there a more elegant way of working around sprintf on Windows 7. I've heard of sprintf_s, but a little reading told me that it isn't compatible with Windows 7.

    The line of code I'm having trouble with is:

    Code:
    sprintf(Pxi6509::DeviceDescriptor, "PXI1Slot%i", DeviceNo);
    I'm very new to this, and would really appreciate the help. I hope this is the right forum to ask this question to.

    Thanks,

    Nilotpal

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    First insure that the size of your Pxi6509:eviceDescriptor is large enough to hold your new C-string.

    Since sprintf_s is a Microsoft function it should work just fine with your compiler.

    Jim

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    10
    Thanks for the reply. I made a few changes to the array definitions and am tried to use the 'sprintf_s' statements, but I keep getting the following error on the 'sprintf_s':

    error C3861: 'sprintf_s': identifier not found, even with argument-dependent lookup

    What am I doing wrong? I have included 'stdio.h' in the code. Is there a different header file for 'sprintf_s'? Or a later version for Windows 7, which I have to install/add to project. The 'stdio.h' header that I have doesn't have any definiton for 'sprintf_s' in it

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I'm sorry it looks like sprintf_s was not added until Visual 2005 so you will need to either upgrade your compiler or use sprintf

    Jim

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    10
    Yep, but unfortunately it will take me a while to upgrade my compiler. And 'sprintf' is still causing problems. Is there any other workaround? Someone told me that marshalling between a string and a character might work, so I'm doing some reading on that. Basically I'm trying to get rid of the 'sprintf' statements. So I would like to do something that can cast the 'DeviceDescriptor' variable, which is actually a char array to a string, so I can re-cast the functions in my code to accept strings (instead of a character array)

    So, I'm assigning the 'DeviceDescriptor' through the following code:

    Code:
    Pxi6509::DeviceDescriptor = String::Format(S"PXI1Slot{0}", DeviceNo.ToString());
    Can I get any help in doing the initial marshalling, casting?

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    If you are using C++ you can use std::string, std::stringstream etc.

    Jim

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    10
    Hey Jim,

    Could you help me on how to use std::string in this context? I have never used string classes before. Basically I want my code to do what the following 'sprintf' statement would have done:
    Code:
    sprintf(Pxi6509::DeviceDescriptor, "PXI1Slot%i", DeviceNo);
    The Pxi6509 constructor takes in the DeviceNo (int) parameter as the input, and I want the DeviceDescriptor object to be stored as "PXI1Slot6" (if DeviceNo is 6).

    I have defined DeviceDescriptor in the header file as:
    Code:
    static String* DeviceDescriptor
    Is there anything wrong with the definition?

    Thanks

    Neil

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    String is the .NET string class, and not the standard string class, right?

    To use sprintf, you'd need a char array. You can use std::string, although I'd probably go a bit farther and use a stringstream.
    Code:
    std::stringstream DeviceDescriptor;
    DeviceDescriptor << "PXI1Slot" << DeviceNo;
    After that point, DeviceDescriptor.str() would give you a std::string object, and if you needed a C-style string you could use DeviceDescriptor.str().c_str().

    Unfortunately my knowledge of .NET is non-existent, but I would imagine there's a converter function to a .NET String object as well.

  9. #9
    Registered User
    Join Date
    Jan 2011
    Posts
    10
    @tabstop: Thanks. From the code you posted, I gather you would require the DeviceDescriptor to be defined as a character array in the header file, say, static char* DeviceDescriptor; Is that correct?

    Also, to use std::stringstream, are there any other 'namespaces' to include in the code? I already have 'using namespace std;' as part of my code.

    I tried using std::stringstream, but I'm obviously not doing it right, coz it refused to compile with way too many errors to even list here. As I said before, the code compiles fine with 'sprintf', but I'm looking for a workaround as 'sprintf' creates problems when I try running it with the hardware

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by neilc View Post
    @tabstop: Thanks. From the code you posted, I gather you would require the DeviceDescriptor to be defined as a character array in the header file, say, static char* DeviceDescriptor; Is that correct?
    Well, no. I define DeviceDescriptor right there in the code, and that's what it should be defined as. If you were planning to use sprintf, then you must define DeviceDescriptor to be a array of chars. If you try to make it a String, then nothing good can come of it.
    Also, to use std::stringstream, are there any other 'namespaces' to include in the code? I already have 'using namespace std;' as part of my code.

    I tried using std::stringstream, but I'm obviously not doing it right, coz it refused to compile with way too many errors to even list here. As I said before, the code compiles fine with 'sprintf', but I'm looking for a workaround as 'sprintf' creates problems when I try running it with the hardware
    No namespaces are required (there aren't any namespaces in standard C++ other than std, and std::tr1 for the new stuff). You need to #include the header file <sstream>.

  11. #11
    Registered User
    Join Date
    Jan 2011
    Posts
    10
    My code will have to use the String .NET class. So, I guess std::stringstream wont help me in the conversion from unmanaged objects to managed code. After doing some reading and tinkering with the code, I've coded to convert the System::String* to Char*. And the string marshaling works fine for a few objects, but the DeviceDescriptor (which I want to convert from a String to a character array) is still giving me the following error:

    error LNK2022: metadata operation failed (80131188) : Inconsistent field declarations in duplicated types (types: Pxi6509; fields: DeviceDescriptorChar): (0x04000014).

    I believe this has something to do with the ordering of header files, but I can't put a finger on which one and where. Hoping somebody would know. I guess I'll start a new topic for "Problem with marshalling strings". because it isn't a 'sprintf' issue anymore (although the main issue was initiated due to 'sprintf' not working well with the hardware on Windows 7.

    Thanks a lot for the inputs anyways. Do let me know if you have any advice when I put in the code on the new topic.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program works on Windows XP and 2000, not on 98 or ME
    By MidnightlyCoder in forum C++ Programming
    Replies: 7
    Last Post: 03-10-2006, 03:36 PM
  2. SDL and Windows
    By nickname_changed in forum Windows Programming
    Replies: 14
    Last Post: 10-24-2003, 12:19 AM
  3. Windows Memory Allocation :: C++
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 11-03-2002, 12:13 PM
  4. Information: Exiting Windows
    By Okiesmokie in forum C++ Programming
    Replies: 2
    Last Post: 05-12-2002, 09:42 AM
  5. shutting down windows
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 01-02-2002, 12:28 PM