Versie 4.10: Winsock object toegevoegd

svn path=/Slnkdwf/trunk/; revision=26204
This commit is contained in:
Jos Groot Lipman
2015-09-05 09:55:56 +00:00
parent 796afbab55
commit 152e6c8705
11 changed files with 391 additions and 9 deletions

View File

@@ -421,6 +421,28 @@ interface IFTP : IDispatch{
[propput, id(11), helpstring("property Flags")] HRESULT Flags([in] LONG newVal);
};
[
object,
uuid(BCC2A24D-B908-46FB-8BEF-4D002CED060D),
dual,
nonextensible,
pointer_default(unique)
]
interface IWinSock : IDispatch{
[propget, id(1)] HRESULT LocalHostIP([out, retval] BSTR* pVal);
[propput, id(1)] HRESULT LocalHostIP([in] BSTR newVal);
[propget, id(2)] HRESULT RemoteHostIP([out, retval] BSTR* pVal);
[propput, id(2)] HRESULT RemoteHostIP([in] BSTR newVal);
[propget, id(3)] HRESULT RemotePort([out, retval] ULONG* pVal);
[propput, id(3)] HRESULT RemotePort([in] ULONG newVal);
[id(4)] HRESULT OpenConnection();
[id(5)] HRESULT SendData([in] BSTR sData, [out, retval] BYTE* bResult);
[id(6)] HRESULT isDataAvailable();
[id(7)] HRESULT ReceiveData([out, retval] BSTR* sData);
[id(8)] HRESULT ShutdownConnection();
[propget, id(9)] HRESULT Timeout([out, retval] ULONG* pVal);
[propput, id(9)] HRESULT Timeout([in] ULONG newVal);
};
[
uuid(B6FCDE6E-141C-4601-B3AC-4DF4D5F25DF8),
version(1.0),
@@ -588,4 +610,11 @@ library SLNKDWFLib
{
[default] interface IFTP;
};
[
uuid(B99C545B-D9F8-4AE7-B044-9CB1B3F7DDC9)
]
coclass WinSock
{
[default] interface IWinSock;
};
};

View File

@@ -516,6 +516,7 @@
<ClCompile Include="Whip2PNG.cpp" />
<ClCompile Include="WhipCleaner.cpp" />
<ClCompile Include="WhipFile.cpp" />
<ClCompile Include="WinSock.cpp" />
<ClCompile Include="Zip.cpp" />
<ClCompile Include="SLNKDWF_i.c">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
@@ -565,6 +566,7 @@
<None Include="Whip2PNG.rgs" />
<None Include="WhipCleaner.rgs" />
<None Include="WhipFile.rgs" />
<None Include="WinSock.rgs" />
<None Include="Zip.rgs" />
</ItemGroup>
<ItemGroup>
@@ -596,6 +598,7 @@
<ClInclude Include="Whip2PNG.h" />
<ClInclude Include="WhipCleaner.h" />
<ClInclude Include="WhipFile.h" />
<ClInclude Include="WinSock.h" />
<ClInclude Include="WriterTest.h" />
<ClInclude Include="Zip.h" />
</ItemGroup>

View File

@@ -103,6 +103,9 @@
<ClCompile Include="SLNKDWF_i.c">
<Filter>Generated Files</Filter>
</ClCompile>
<ClCompile Include="WinSock.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="SLNKDWF.def">
@@ -186,6 +189,9 @@
<None Include="Zip.rgs">
<Filter>Header Files\Resource Files</Filter>
</None>
<None Include="WinSock.rgs">
<Filter>Header Files\Resource Files</Filter>
</None>
</ItemGroup>
<ItemGroup>
<Midl Include="SLNKDWF.idl">
@@ -274,6 +280,9 @@
<ClInclude Include="Zip.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="WinSock.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="SLNKdwf.rc">

View File

@@ -1,6 +1,6 @@
// Zorg dat versies alfabetisch altijd op elkaar volgen!
#define SLNK_MAJOR_VERSION 4
#define SLNK_MINOR_VERSION 02
#define SLNK_MINOR_VERSION 10
#define SLNK_BUILD_VERSION 0
// Define resource strings

Binary file not shown.

View File

@@ -13,13 +13,11 @@
#undef APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
// English (U.S.) resources
// English (United States) resources
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
#ifdef _WIN32
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
#pragma code_page(1252)
#endif //_WIN32
#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
@@ -105,7 +103,7 @@ BEGIN
IDS_PROJNAME "SLNKDWF"
END
#endif // English (U.S.) resources
#endif // English (United States) resources
/////////////////////////////////////////////////////////////////////////////
@@ -113,10 +111,8 @@ END
// Dutch (Netherlands) resources
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_NLD)
#ifdef _WIN32
LANGUAGE LANG_DUTCH, SUBLANG_DUTCH
#pragma code_page(1252)
#endif //_WIN32
/////////////////////////////////////////////////////////////////////////////
//
@@ -142,6 +138,7 @@ IDR_BARCODE REGISTRY "Barcode.rgs"
IDR_QRCODE REGISTRY "QRCode.rgs"
IDR_CRYPTO REGISTRY "Crypto.rgs"
IDR_FTP REGISTRY "FTP.rgs"
IDR_WINSOCK REGISTRY "WinSock.rgs"
#endif // Dutch (Netherlands) resources
/////////////////////////////////////////////////////////////////////////////

233
SlnkDWFCom/WinSock.cpp Normal file
View File

@@ -0,0 +1,233 @@
// WinSock.cpp : Implementation of CWinSock
#include "stdafx.h"
#include "WinSock.h"
// Gebaseerd op http://www.binarytides.com/winsock-socket-programming-tutorial/
// CWinSock
STDMETHODIMP CWinSock::InterfaceSupportsErrorInfo(REFIID riid)
{
static const IID* const arr[] =
{
&IID_IWinSock
};
for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
{
if (InlineIsEqualGUID(*arr[i],riid))
return S_OK;
}
return S_FALSE;
}
STDMETHODIMP CWinSock::get_LocalHostIP(BSTR* pVal)
{
CComBSTR bstrString(m_LocalHostIP);
return bstrString.CopyTo(pVal);
}
STDMETHODIMP CWinSock::put_LocalHostIP(BSTR newVal)
{
m_LocalHostIP = newVal;
return S_OK;
}
STDMETHODIMP CWinSock::get_RemoteHostIP(BSTR* pVal)
{
CComBSTR bstrString(m_RemoteHostIP);
return bstrString.CopyTo(pVal);
}
STDMETHODIMP CWinSock::put_RemoteHostIP(BSTR newVal)
{
m_RemoteHostIP = newVal;
return S_OK;
}
STDMETHODIMP CWinSock::get_RemotePort(ULONG* pVal)
{
(*pVal) = m_RemotePort;
return S_OK;
}
STDMETHODIMP CWinSock::put_RemotePort(ULONG newVal)
{
m_RemotePort = newVal;
return S_OK;
}
bool myconnect(SOCKET &sock, struct sockaddr_in &address, int timeout)
{
TIMEVAL Timeout;
Timeout.tv_sec = (long)(timeout / 1000);
Timeout.tv_usec = timeout % 1000;
// sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
//set the socket in non-blocking
unsigned long iMode = 1;
int iResult = ioctlsocket(sock, FIONBIO, &iMode);
if (iResult != NO_ERROR)
{
return false;
//printf("ioctlsocket failed with error: %ld\n", iResult);
}
if (!connect(sock, (struct sockaddr *)&address, sizeof(address)))
{
return false;
}
// restart the socket mode
iMode = 0;
iResult = ioctlsocket(sock, FIONBIO, &iMode);
if (iResult != NO_ERROR)
{
//printf("ioctlsocket failed with error: %ld\n", iResult);
return false;
}
fd_set Write, Err;
FD_ZERO(&Write);
FD_ZERO(&Err);
FD_SET(sock, &Write);
FD_SET(sock, &Err);
// check if the socket is ready
select(0, NULL, &Write, &Err, &Timeout);
if (FD_ISSET(sock, &Write))
{
return true;
}
return false;
}
STDMETHODIMP CWinSock::OpenConnection()
{
WSADATA wsa;
struct sockaddr_in server;
// Initialize Winsock API
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
{
return myAtlReportError(GetObjectCLSID(), "\nCWinSock: Could not startup %d", WSAGetLastError());
}
// Create new socket
//if ((m_socket = WSASocketW(AF_INET, SOCK_STREAM, IPPROTO_TCP, 0, 0, WSA_FLAG_OVERLAPPED)) == INVALID_SOCKET)
if ((m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET)
{
return myAtlReportError(GetObjectCLSID(), "\nCWinSock: Could not create socket : %d", WSAGetLastError());
}
int nTimeout = m_Timeout; // 3 seconds
setsockopt(m_socket, SOL_SOCKET, SO_SNDTIMEO, (const char*)&nTimeout, sizeof(int));
setsockopt(m_socket, SOL_SOCKET, SO_RCVTIMEO, (const char*)&nTimeout, sizeof(int));
InetPton(AF_INET, m_RemoteHostIP, &server.sin_addr.s_addr);
server.sin_family = AF_INET;
server.sin_port = htons((u_short)(m_RemotePort));
// Connect to remote server
//if (connect(m_socket, (struct sockaddr *)&server, sizeof(server)) < 0)
if (!myconnect(m_socket, server, m_Timeout))
{
return myAtlReportError(GetObjectCLSID(), "\nCWinSock: Could not connect %d", WSAGetLastError());
}
/* 'Bind socket to LocalHostIP
msaLocalAddr.sin_family = PF_INET
msaLocalAddr.sin_port = 0
msaLocalAddr.sin_addr.S_addr = inet_addr(mstrLocalHostIP)
If(msaLocalAddr.sin_addr.S_addr = INADDR_NONE) Then
SetLastErrorCode "Error in OpenConnection::inet_addr"
Exit Function
End If
WSAResult = bind(mlngSocket, msaLocalAddr, Len(msaLocalAddr))
If(WSAResult = SOCKET_ERROR) Then
SetLastErrorCode "Error in OpenConnection::bind"
Exit Function
End If
*/
return S_OK;
}
STDMETHODIMP CWinSock::SendData(BSTR sData, BYTE* bResult)
{
//Send some data
CString message(sData);
if (send(m_socket, message, message.GetLength(), 0) < 0)
{
return myAtlReportError(GetObjectCLSID(), "\nCWinSock: Send failed %d", WSAGetLastError());
}
return S_OK;
}
STDMETHODIMP CWinSock::isDataAvailable()
{
// TODO: Add your implementation code here
return S_OK;
}
STDMETHODIMP CWinSock::ReceiveData(BSTR* sData)
{
char server_reply[8192];
int recv_size;
//Receive a reply from the server
if ((recv_size = recv(m_socket, server_reply, sizeof(server_reply), 0)) == SOCKET_ERROR)
{
return myAtlReportError(GetObjectCLSID(), "\nCWinSock: Recv failed %d", WSAGetLastError());
}
//Add a NULL terminating character to make it a proper string before printing
if (recv_size >= sizeof(server_reply))
server_reply[sizeof(server_reply) - 1] = '\0';
else
server_reply[recv_size] = '\0';
CComBSTR bstrString(server_reply);
return bstrString.CopyTo(sData);
}
STDMETHODIMP CWinSock::ShutdownConnection()
{
shutdown(m_socket, SD_BOTH);
closesocket(m_socket);
WSACleanup();
return S_OK;
}
STDMETHODIMP CWinSock::get_Timeout(ULONG* pVal)
{
(*pVal) = m_Timeout;
return S_OK;
}
STDMETHODIMP CWinSock::put_Timeout(ULONG newVal)
{
m_Timeout = newVal;
return S_OK;
}

84
SlnkDWFCom/WinSock.h Normal file
View File

@@ -0,0 +1,84 @@
// WinSock.h : Declaration of the CWinSock
#pragma once
#include "resource.h" // main symbols
#include <Ws2tcpip.h>
#pragma comment(lib,"ws2_32.lib") //Winsock Library
#include "SLNKDWF.h"
#if defined(_WIN32_WCE) && !defined(_CE_DCOM) && !defined(_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA)
#error "Single-threaded COM objects are not properly supported on Windows CE platform, such as the Windows Mobile platforms that do not include full DCOM support. Define _CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA to force ATL to support creating single-thread COM object's and allow use of it's single-threaded COM object implementations. The threading model in your rgs file was set to 'Free' as that is the only threading model supported in non DCOM Windows CE platforms."
#endif
using namespace ATL;
// CWinSock
class ATL_NO_VTABLE CWinSock :
public CComObjectRootEx<CComSingleThreadModel>,
public CComCoClass<CWinSock, &CLSID_WinSock>,
public ISupportErrorInfo,
public IDispatchImpl<IWinSock, &IID_IWinSock, &LIBID_SLNKDWFLib, /*wMajor =*/ 1, /*wMinor =*/ 0>
{
public:
CWinSock()
{
}
DECLARE_REGISTRY_RESOURCEID(IDR_WINSOCK)
BEGIN_COM_MAP(CWinSock)
COM_INTERFACE_ENTRY(IWinSock)
COM_INTERFACE_ENTRY(IDispatch)
COM_INTERFACE_ENTRY(ISupportErrorInfo)
END_COM_MAP()
// ISupportsErrorInfo
STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid);
DECLARE_PROTECT_FINAL_CONSTRUCT()
HRESULT FinalConstruct()
{
return S_OK;
}
void FinalRelease()
{
}
public:
STDMETHOD(get_LocalHostIP)(BSTR* pVal);
STDMETHOD(put_LocalHostIP)(BSTR newVal);
STDMETHOD(get_RemoteHostIP)(BSTR* pVal);
STDMETHOD(put_RemoteHostIP)(BSTR newVal);
STDMETHOD(get_RemotePort)(ULONG* pVal);
STDMETHOD(put_RemotePort)(ULONG newVal);
STDMETHOD(OpenConnection)();
STDMETHOD(SendData)(BSTR sData, BYTE* bResult);
STDMETHOD(isDataAvailable)();
STDMETHOD(ReceiveData)(BSTR* sData);
STDMETHOD(ShutdownConnection)();
private:
SOCKET m_socket;
CString m_LocalHostIP;
CString m_RemoteHostIP;
ULONG m_RemotePort;
ULONG m_Timeout;
public:
STDMETHOD(get_Timeout)(ULONG* pVal);
STDMETHOD(put_Timeout)(ULONG newVal);
};
OBJECT_ENTRY_AUTO(__uuidof(WinSock), CWinSock)

26
SlnkDWFCom/WinSock.rgs Normal file
View File

@@ -0,0 +1,26 @@
HKCR
{
SLNKDWF.WinSock.1 = s 'WinSock Class'
{
CLSID = s '{B99C545B-D9F8-4AE7-B044-9CB1B3F7DDC9}'
}
SLNKDWF.WinSock = s 'WinSock Class'
{
CurVer = s 'SLNKDWF.WinSock.1'
}
NoRemove CLSID
{
ForceRemove {B99C545B-D9F8-4AE7-B044-9CB1B3F7DDC9} = s 'WinSock Class'
{
ProgID = s 'SLNKDWF.WinSock.1'
VersionIndependentProgID = s 'SLNKDWF.WinSock'
ForceRemove Programmable
InprocServer32 = s '%MODULE%'
{
val ThreadingModel = s 'Apartment'
}
TypeLib = s '{B6FCDE6E-141C-4601-B3AC-4DF4D5F25DF8}'
Version = s '1.0'
}
}
}

View File

@@ -32,6 +32,7 @@
#define IDR_QRCODE 132
#define IDR_CRYPTO 133
#define IDR_FTP 134
#define IDR_WINSOCK 135
// Next default values for new objects
//
@@ -40,6 +41,6 @@
#define _APS_NEXT_RESOURCE_VALUE 201
#define _APS_NEXT_COMMAND_VALUE 32768
#define _APS_NEXT_CONTROL_VALUE 201
#define _APS_NEXT_SYMED_VALUE 135
#define _APS_NEXT_SYMED_VALUE 136
#endif
#endif

View File

@@ -17,7 +17,7 @@
#endif
#ifndef _WIN32_WINNT // Allow use of features specific to Windows XP or later.
#define _WIN32_WINNT 0x0501 // Change this to the appropriate value to target other versions of Windows.
#define _WIN32_WINNT 0x0600 // Vista/2008 Change this to the appropriate value to target other versions of Windows.
#endif
#ifndef _WIN32_WINDOWS // Allow use of features specific to Windows 98 or later.