Call dll in MT4, but no console interface come out,WHY?

 

GUYS:

I try to test dll-calling in MQL4, so I build a test dll as following:

#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <time.h>
#include <iostream>

using namespace std;

extern "C" __declspec(dllexport) void HelloWorld();

void HelloWorld()
{
        cout<<"Hello,World!"<<endl;
        getchar();
        //----
        FILE *fp = NULL;
        fp = fopen("d:\\test.txt","w");
        return;
}

What it does is very simple:

1. Output "Hello,World!" from a console interface, and then wait me to press keys;

2. Create a test.txt file in d:\;

And then I use following script to load the dll:

#property copyright "Saji.Ren"
#property link      "GoldenHeart Investment"

//----------
#import "helloworldDLL.dll"
 void HelloWorld();
#import

//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
{
   HelloWorld();
   return(0);
}
//+------------------------------------------------------------------+

And I got interesting results:

1. There is no ERROR messages appear, but also NO console interface appears;

2. But the test.txt is newly created in d:\;

3. I check the log of MT4, which says:

09:00:05 MetaTrader 4 4.00 build 409 started (MetaQuotes Software Corp.)
09:00:56 Script CallHelloWorld NZDUSD,M15: loaded successfully
09:00:58 Script CallHelloWorld NZDUSD,M15: removed


So I think:

1. The helloworldDLL.dll is successfully called. (so there created the txt file)

2. There's some reason cause the console interface cannot comes out. (I didn't see it, and the getchar() will prevent the console window disappear too quickly thus I can't see it with naked-eyes)

Can anyone tell me the reason?

Thank you in advanced!

Saji from Shanghai

 
saji:

1. There is no ERROR messages appear, but also NO console interface appears;

Win32 GUI apps don't, by default, have a console. If you want to do console output from inside a GUI app then you explicitly need to allocate a console.

For example, from http://stackoverflow.com/questions/3009042/how-to-view-printf-output-in-a-win32-application-on-visual-studio-2010:

void SetStdOutToNewConsole()
{
    int hConHandle;
    long lStdHandle;
    FILE *fp;

    // Allocate a console for this app
    AllocConsole();

    // Redirect unbuffered STDOUT to the console
    lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);
    hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
    fp = _fdopen(hConHandle, "w");
    *stdout = *fp;

    setvbuf(stdout, NULL, _IONBF, 0);
}

After using SetStdOutToNewConsole(), any subsequent output from your DLL will then appear in a console window. For example:

extern "C" void __stdcall Test()
{
        SetStdOutToNewConsole();
        printf("Test message\r\n");
}
 
jjc:

Win32 GUI apps don't, by default, have a console. If you want to do console output from inside a GUI app then you explicitly need to allocate a console.

For example, from http://stackoverflow.com/questions/3009042/how-to-view-printf-output-in-a-win32-application-on-visual-studio-2010:

Thank you, jjc.

You give the exactly right answer, and also the links helped a lot.

 
jjc:

Win32 GUI apps don't, by default, have a console. If you want to do console output from inside a GUI app then you explicitly need to allocate a console.

For example, from http://stackoverflow.com/questions/3009042/how-to-view-printf-output-in-a-win32-application-on-visual-studio-2010:

After using SetStdOutToNewConsole(), any subsequent output from your DLL will then appear in a console window. For example:

Also, I have anther question concerning the dll-calling in MT4.

Can you go there and have a look?

I think you are a real experts in this field.

Thank you for your help!

Reason: