LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

cvi visual studio printf

Solved!
Go to solution

I'm trying to use printf with a cvi project in Visual Studio. I get the stdio window but my printf & gets statements seem to be ignored. I enclosed the code between 2 popups. The 2 popups are displayed, the code in between is being ignored. Anyone know : What am I missing ? here's the code :

#include <cvirte.h>
#include <ansi_c.h>
#include <userint.h>
#include <utility.h>
#include <gpib.h>

int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
{
char instr[20];

// Initialize the CVI Run-time Engine
if (InitCVIRTE(hInstance, 0, 0) == 0) return -1;
MessagePopup("", "Start");
SetStdioPort(HOST_SYSTEM_STDIO);
SetStdioWindowVisibility(1);
printf("Enter Command\n");
gets(instr);
MessagePopup("","Done");
}
0 Kudos
Message 1 of 8
(5,915 Views)
Solution
Accepted by topic author pblanco

Hello pblanco!

 

Since you are using an external compiler, such as Microsoft Visual Studio, the following KB applies:

http://digital.ni.com/public.nsf/websearch/862567530005F09C862565BB005FA610

 

According to the document, you use FmtOut() to print to the redirected standard I/O. #include <fromatio.h> to use this function.

 

Best regards!

- Johannes

0 Kudos
Message 2 of 8
(5,902 Views)

Thank You

0 Kudos
Message 3 of 8
(5,899 Views)

Thanks Johannes, working fine.

0 Kudos
Message 4 of 8
(5,886 Views)

You are welcome!

 

To add some background, the reason why your initial code using printf didn't work is because when you are using Microsoft Visual C, your application doesn't link with the CVI implementation of the ANSI C library, as it does when you build from LabWindows/CVI. In your case, the program is linking against MSVC's implementation of printf, which doesn't have the understanding of the CVI standard I/O mechanism.

 

Best regards!

- Johannes

0 Kudos
Message 5 of 8
(5,848 Views)

OK, thanks for the extra info, but then what does the printf actually do in MS, I'm a little confused. Does it have to do with console window vs stdio window ? If so,  What's the diff ?

0 Kudos
Message 6 of 8
(5,833 Views)

When you're using FmtOut, you are using the CVI standard I/O, which is different from that of MSVC. VC's printf doesn't know anything about the CVI standard I/O, so it writes to whatever console VC sets up as the application's console.

 

In your case, I suspect that you started creating your application using the LabWindows/CVI Project wizard. The project template creates for you a Windows application that doesn't have a console. In order to be able to output to VC's system console, you can change the application SubSystem property from WINDOWS(/SUBSYSTEM:WINDOWS) to CONSOLE(/SUBSYSTEM:CONSOLE).

You can find the setting in the VC project Property Pages: Configuration Properties » Linker » System.

Read more about the subsystem specification at MSDN: https://msdn.microsoft.com/en-us/library/fcc1zstk.aspx

 

Changing the subsystem to a console, you can use both MSVC's printf and FmtOut to print to the same console.

 

- Johannes

0 Kudos
Message 7 of 8
(5,818 Views)

Johannes,

 

That's great, thanks for explaining. I'll take a look at this.

I'm just getting familiar with all the possibilties within Visual Studio after working with CVI for many years.

 

Thanks again, be well.

0 Kudos
Message 8 of 8
(5,796 Views)