Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Beginner Networking
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:
 
//+------------------------------------------------------------------+
//| 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 8-May-10 13:30pm
Rezame951
Edited 8-May-10 22:30pm
Moak11.4K
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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:
#pragma comment(lib, "Wininet")
  Permalink  
Comments
Dmitry Vitkovsky at 9-May-10 4:56am
   
This is the answer to your second question (see Answer2)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 195
1 ProgramFOX 130
2 Maciej Los 105
3 Sergey Alexandrovich Kryukov 105
4 Afzaal Ahmad Zeeshan 82
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 18 May 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100