Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Dear Sirs,

I'm using the below code to make a DLL, but have encountered some compilation errors I don't know how to fix:

C++
//+------------------------------------------------------------------+
//| Sample DLL for MQL4 |
//| Copyright © 2004-2006, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <wininet.h>
#include <atlstr.h>
#include <string>

//----
#define MT4_EXPFUNC __declspec(dllexport)
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
MT4_EXPFUNC int __stdcall Sample( int Whatever )
{
//---- Declare Return Value
CHAR buffer[2048] ;
CString m_strContents;
DWORD dwRead;

/* Connect to the internet */
HINTERNET hiNet = InternetOpen(
L"InetURL/1.0",
INTERNET_OPEN_TYPE_PRECONFIG,
NULL,
NULL,
0
);
/* if connection fails throw error */
if( !hiNet ) { return( -1010 ); }

/* Connect to a site */
HINTERNET hConnection = InternetConnect(
hiNet,
L"www.yoursite.com",
INTERNET_DEFAULT_HTTP_PORT,
NULL,
NULL,
INTERNET_SERVICE_HTTP,
0,
0
);
/* if connetion to site failed */
if( !hConnection )
{
InternetCloseHandle(hiNet);
return( -1020 );
} // COULD NOT CONNECT TO WEBSITE

/* Get Data */
HINTERNET hData = HttpOpenRequest( hConnection, L"GET", L"/yourpage.php", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0 );
if ( !hData )
{
InternetCloseHandle(hConnection);
InternetCloseHandle(hiNet);
return( -1030 ); // PAGE NOT FOUND
}

HttpSendRequest( hData, NULL, 0, NULL, 0);
bool Done = false;
while( !Done )
{
InternetReadFile( hData, buffer, 255, &dwRead );
if ( dwRead == 0 ) { Done = true; }
buffer[dwRead] = 0;
m_strContents += buffer;
}

//---- Here you can output m_strContents
MessageBox( 0, m_strContents, L"WebPage OutPut", 0 );

InternetCloseHandle(hConnection);
InternetCloseHandle(hiNet);
InternetCloseHandle(hData);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+


Errors are as below:

1>------ Build started: Project: ExpertSample, Configuration: Debug Win32 ------
1>Compiling...
1>ExpertSample.cpp
1>c:\users\reza\desktop\555\3\dllsample\expertsample.cpp(41) : error C2664: 'InternetOpenA' : cannot convert parameter 1 from 'const wchar_t [12]' to 'LPCSTR'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\reza\desktop\555\3\dllsample\expertsample.cpp(55) : error C2664: 'InternetConnectA' : cannot convert parameter 2 from 'const wchar_t [17]' to 'LPCSTR'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\reza\desktop\555\3\dllsample\expertsample.cpp(64) : error C2664: 'HttpOpenRequestA' : cannot convert parameter 2 from 'const wchar_t [4]' to 'LPCSTR'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\reza\desktop\555\3\dllsample\expertsample.cpp(83) : error C2664: 'MessageBoxA' : cannot convert parameter 3 from 'const wchar_t [15]' to 'LPCSTR'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>Build log was saved at "file://C:\Users\Reza\Desktop\555\3\DLLSample\Debug\BuildLog.htm"
1>ExpertSample - 4 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


How do I solve these errors?

Thanks.
Posted
Updated 8-May-10 21:30pm
v3

The functions that are named in the error messages are expecting non wide-character strings, but you are specifying wide-character strings by prefixing them with 'L'.

Take away the L before your string literals, and it should compile.
 
Share this answer
 
You should reference this lib:
Wininet.lib

There are 2 ways to do this:

1. Add this lib in 'Linker' settings in project properties
2. Add the following into your code:
C++
#pragma comment(lib, "Wininet")
 
Share this answer
 
Comments
Dimitri Witkowski 9-May-10 4:56am    
This is the answer to your second question (see Answer2)
Dear Sir,
thank you for your reply.
After delete L from my code it compiled.
After that I tried build dll for that and when tried build that I met below errors:

1>------ Build started: Project: ExpertSample, Configuration: Debug Win32 ------
1>Linking...
1> Creating library .\Debug/ExpertSample.lib and object .\Debug/ExpertSample.exp
1>ExpertSample.obj : error LNK2019: unresolved external symbol __imp__InternetReadFile@16 referenced in function "int __stdcall Sample(int)" (?Sample@@YGHH@Z)
1>ExpertSample.obj : error LNK2019: unresolved external symbol __imp__HttpSendRequestA@20 referenced in function "int __stdcall Sample(int)" (?Sample@@YGHH@Z)
1>ExpertSample.obj : error LNK2019: unresolved external symbol __imp__HttpOpenRequestA@32 referenced in function "int __stdcall Sample(int)" (?Sample@@YGHH@Z)
1>ExpertSample.obj : error LNK2019: unresolved external symbol __imp__InternetCloseHandle@4 referenced in function "int __stdcall Sample(int)" (?Sample@@YGHH@Z)
1>ExpertSample.obj : error LNK2019: unresolved external symbol __imp__InternetConnectA@32 referenced in function "int __stdcall Sample(int)" (?Sample@@YGHH@Z)
1>ExpertSample.obj : error LNK2019: unresolved external symbol __imp__InternetOpenA@20 referenced in function "int __stdcall Sample(int)" (?Sample@@YGHH@Z)
1>.\Debug/ExpertSample.dll : fatal error LNK1120: 6 unresolved externals
1>Build log was saved at "file://c:\Users\Reza\Desktop\555\3\DLLSample\Debug\BuildLog.htm"
1>ExpertSample - 7 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


I must add any linker to this?
How I can modify this?

Thanks.
 
Share this answer
 
dear Sirs,
Thank you for your reply.
Now it works.
Another question:
How I return "WebPage OutPut" in MessageBox or read that?
I mean how I can use that for example for printing or use in other section my program?

Thanks.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900