How to link RtlIpv4StringToAddressExW function? - winapi

I am not able to link RtlIpv4StringToAddressExW(). This is a simplified version of my program.
#include <WinSock2.h>
#pragma comment(lib, "ws2_32.lib")
#include <Windows.h>
#include <cstdio>
#include <MSTcpIP.h>
HRESULT doMAin()
{
LONG error;
PSOCKADDR_IN sin4;
error = RtlIpv4StringToAddressExW(
L"127.0.0.1",
TRUE,
&sin4->sin_addr,
&sin4->sin_port);
return S_OK;
}
And the error I am getting is:
main.obj : error LNK2001: unresolved external symbol __imp__RtlIpv4StringToAddressExW#16
Does anyone know what could be wrong?

"An import library containing the RtlIpv4StringToAddressEx function is not included in the Microsoft Windows Software Development Kit (SDK) released for Windows Vista. The RtlIpv4StringToAddressEx function is included in the Ntdll.lib import library included in the Windows Driver Kit (WDK). An application could also use the GetModuleHandle and GetProcAddress functions to retrieve the function pointer from the Ntdll.dll and call this function."
from the docs.
If there was a lib to pragma comment, it would be Ntdll.lib. To repeat the docs, you can either grab the one from the DDK, or GetProcAddress the sucker.
GetProcAddressing would look like
typedef LONG (NTAPI *pfRtlIpv4StringToAddressEx)(PCWSTR,BOOLEAN,IN_ADDR *,PUSHORT);
pfRtlIpv4StringToAddressEx pRtlIpv4StringToAddressEx = (pfRtlIpv4StringToAddressEx)GetProcAddress(GetModuleHandle("ntdll.dll"), "RtlIpv4StringToAddressExW");
error = (*pRtlIpv4StringToAddressEx)(
L"127.0.0.1",
TRUE,
&sin4->sin_addr,
&sin4->sin_port);

Related

Unable to link the 64bit ftd2xx static library with project dispaying linker error. How to link the ftd2xx 64bit library to the code

I'm trying to link the ftd2xx.lib (amd64 https://www.ftdichip.com/Drivers/CDM/CDM%20v2.12.28%20WHQL%20Certified.zip) static file to my test program in codeblocks. I'm running codeblocks(mingw) on a intel i3-7020, x64 based processor. I'm getting the linker error obj\Debug\main.o:main.cpp|| undefined reference to `_imp__FT_ListDevices#12'. Can someone please tell me how to link the static library of ftd2xx.lib 64bit version in codeblocks.
#include <iostream>
#include "ftd2xx.h"
using namespace std;
int main()
{
FT_STATUS ftStatus;
int numDevs;
ftStatus = FT_ListDevices(&numDevs,NULL,FT_LIST_NUMBER_ONLY);
cout<<numDevs<<endl;
return 0;
}
It is supposed to compile successfully and display the number of devices connected.

error C4430: missing type specifier - int assumed?

I have written a callback method and when I am trying to compile the project in vs2012, I am getting the following error:
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2143: syntax error : missing ',' before '&'
the both error present in same line.
These are the following code which I am trying:
#include <windows.h>
#include "atlstr.h"
///////////////////////////////////////////////////////////////////////////////
class CInf;
typedef BOOL (*PENUMDEVCALLBACK)(CInf* inf, const CString& devname, const CString& instsecname, const CStringArray& devid, PVOID context, DWORD& code);
I think the error will be present in CStringArray& argument because when I include the afxcoll.h.
the fatal error C1189: #error : WINDOWS.H already included. MFC apps must not #include <windows.h>.
When I removed the windows.h. the above error gone but I need windows.h header also.
Any idea how to include windows.h and afxcoll.h in same file.
It is not possible to include only isolated parts of the MFC. When you want to use CStringArray you Need to include the afx.h or afxwin.h file.
It is possible to use CString alone. If you just want an Array of CString and you don't Need the complete MFC than it is easier to use std::vector. CString works perfect with all STL container.

`Ole2.h` include causing an error

I'm using Microsoft Visual C++ Express 2012. I have the following as part of my header setup:
#include <windows.h>
#include <Ole2.h>
When I include windows.h on it's own there's no build errors, but when I include Ole2.h either with windows.h or without windows.h I get the following compiler error:
Error 1 error C2628: '_RPC_ASYNC_NOTIFICATION_INFO::$UnnamedClass$0x1c06c483$29$' followed by 'int' is illegal (did you forget a ';'?) C:\Program Files (x86)\Windows Kits\8.0\Include\shared\rpcasync.h 127 1 Test`
Any ideas what might be going wrong? Ultimately I'm trying to write some simple code to interface with Kinect, but even without any Kinect includes present the above error is still generated.
When I double click the error I am taken to rpcasync.h and the following code:
#if !defined(RPC_NO_WINDOWS_H)
//
// Notification by window message
//
struct {
HWND hWnd;
UINT Msg;
} HWND; //<<<-------- the error points here (HWND is underlined in red)
#endif // RPC_NO_WINDOWS_H
PS The reason I'm including Ole2.h at all is because I'm following this tutorial.

Linking to winmm.dll in Visual Studio 2013 Express for mciSendString

I am trying to use mciSendString in visual studio express 2013 (Visual C++) but I keep getting an error
Error 1 error C3861: 'mciSendStringA': identifier not found
I assume this i because I have not linked to the correct dll, but I cannot find any details online or on msdn about how to link to the dll. It seems quite strange that there wouldn't be more obvious documentation about this. Can someone tell me how to link to the dll?
EDIT:
Here is the code I am trying to run:
#include <Windows.h>
#include <iostream>
#include <mmsystem.h>
extern char command1[] = "open C:\\boing.mp3 type MPEGVideo alias 0";
extern char command2[] = "play 0 from 0";
int main()
{
mciSendStringA(command1, NULL, 0, 0);
mciSendStringA(command2, NULL, 0, 0);
}
To make mciSendString() to work, you need to link to winmm.lib.
Just adding winmm.lib to Project Properties > Linker > Input > Additional Dependencies will be fine.
Looking at mmsystem.h (admittedly from the V7.1A Windows SDK, which is the most recent I have installed), I can see that there's a #ifdef _WIN32 block in there. If _WIN32 is not defined, then mciSendStringA is not declared. Instead mciSendString is declared.
Check your project options and ensure that both WIN32 and _WIN32 are defined. I'm guessing that you started from a console project, rather than a Windows Application project, and that at least one of those isn't defined.

Write C DLL for MATLAB

I'm trying to write and compile some C code which I would use frm MATLAB with VS 2012
Here is my header file:
#ifndef _DLLTEST_H_
#define _DLLTEST_H_
#include <iostream>
#include <stdio.h>
#include <windows.h>
extern "C" __declspec(dllexport) int Add(int a, int b);
#endif
And here is implementation:
#include "stdafx.h"
#include "nureader.h"
extern "C" __declspec(dllexport) int Add(int a, int b)
{
return (a + b);
}
Compilation goes fine, but when I try to load DLL to MATLAB, I getting a strange error:
>> [a,b] = loadlibrary('nureader.dll', 'nureader.h')
Error using loadlibrary (line 419)
Failed to preprocess the input file.
Output from preprocessor is:nureader.h
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\eh.h(27) : fatal error C1189: #error : "eh.h
is only for C++!"
Take a look at VS output
fatal error C1189: #error : "eh.h is only for C++!"
You want to write a C library, right? so don't include C++ in it. or compile with G++ but since you're using windows I don't think you have that option...
In any case, track down what includes "eh.h" and try without it. If it builds without it - great, if not - you will need to only isolate the C portion of your program. By looking at the code, you don't seem to need anything more than
#include <stdio.h>
#include <windows.h>
So try that.

Resources