Oh boy...I am still struggling with this.
Now I'm at the stage that I created a WIN32 project in my solution, and
created the C++ file below. The code from the 'createIpForwar dEntry()'
method is from an online example that I found. The code compiles, but
it doesn't create a DLL or so, so I wouldn't know how to invoke this
DLL and test it.
Still, I really wonder what I'm doing wrong when invoking iphlpapi.dll
directly from C# as discussed here:
http://groups-beta.google.com/group/...9e27fc9415d06e
There must be others that have tried this...
What are the other places to search for info on these matters?
====== C++ FILE =======
#include "stdafx.h"
#ifdef _MANAGED
#pragma managed(push, off)
#endif
#ifdef __cplusplus
extern "C" {
#endif
typedef DWORD (*getIpForwardT ableFunction)(P MIB_IPFORWARDTA BLE, PULONG,
BOOL);
typedef DWORD (*createIpForwa rdEntryFunction )(PMIB_IPFORWAR DROW);
typedef DWORD (*setIpForwardE ntryFunction)(P MIB_IPFORWARDRO W);
typedef DWORD (*deleteIpForwa rdEntryFunction )(PMIB_IPFORWAR DROW);
HINSTANCE hinstLib;
getIpForwardTab leFunction myGetIpForwardT able = NULL;
createIpForward EntryFunction myCreateIpForwa rdEntry = NULL;
setIpForwardEnt ryFunction mySetIpForwardE ntry = NULL;
deleteIpForward EntryFunction myDeleteIpForwa rdEntry = NULL;
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_c all,
LPVOID lpReserved
) {
LPCWSTR dllName = LPCWSTR("Iphlpa pi.dll");
hinstLib = LoadLibrary(dll Name);
if (hinstLib == NULL) {
printf("Error: could not load Iphlpapi.dll\n" );
return FALSE;
}
myGetIpForwardT able = (getIpForwardTa bleFunction)
GetProcAddress( hinstLib, "GetIpForwardTa ble");
if (myGetIpForward Table == NULL) {
printf("Error: could not find function getipforwardtab le");
return FALSE;
}
myCreateIpForwa rdEntry = (createIpForwar dEntryFunction)
GetProcAddress( hinstLib, "CreateIpForwar dEntry");
if (myCreateIpForw ardEntry == NULL) {
printf("Error: could not find function createipforward entry");
return FALSE;
}
mySetIpForwardE ntry = (setIpForwardEn tryFunction)
GetProcAddress( hinstLib, "SetIpForwardEn try");
if (mySetIpForward Entry == NULL) {
printf("Error: could not find function setipforwardent ry");
return FALSE;
}
myDeleteIpForwa rdEntry = (deleteIpForwar dEntryFunction)
GetProcAddress( hinstLib, "DeleteIpForwar dEntry");
if (myDeleteIpForw ardEntry == NULL) {
printf("Error: could not find function deleteipforward entry");
return FALSE;
}
return TRUE;
}
__declspec(dlle xport) void __stdcall createIpForward Entry() {
PMIB_IPFORWARDT ABLE pIpForwardTable = NULL;
PMIB_IPFORWARDR OW pRow = NULL;
DWORD dwSize = 0;
BOOL bOrder = FALSE;
DWORD dwStatus = 0;
DWORD NewGateway = 0xDDBBCCAA; // this is in host order Ip Address
AA.BB.CC.DD is DDCCBBAA
// Find out how big our buffer needs to be.
dwStatus = myGetIpForwardT able(pIpForward Table, &dwSize, bOrder);
if (dwStatus == ERROR_INSUFFICI ENT_BUFFER) {
// Allocate the memory for the table
if (!(pIpForwardTa ble = (PMIB_IPFORWARD TABLE)malloc(dw Size))) {
printf("Malloc failed. Out of memory.\n");
exit(1);
}
// Now get the table.
dwStatus = myGetIpForwardT able(pIpForward Table, &dwSize, bOrder);
}
if (dwStatus != ERROR_SUCCESS) {
printf("getIpFo rwardTable failed.\n");
if (pIpForwardTabl e)
free(pIpForward Table);
exit(1);
}
// Search for the row in the table we want. The default gateway has a
destination
// of 0.0.0.0. Notice that we continue looking through the table, but
copy only
// one row. This is so that if there happen to be multiple default
gateways, we can
// be sure to delete them all.
for (unsigned long int i=0; i < pIpForwardTable->dwNumEntries ; i++) {
if (pIpForwardTabl e->table[i].dwForwardDest == 0) {
// We have found the default gateway.
if (!pRow) {
// Allocate some memory to store the row in; this is easier than
filling
// in the row structure ourselves, and we can be sure we change
only the
// gateway address.
pRow = (PMIB_IPFORWARD ROW)malloc(size of(MIB_IPFORWAR DROW));
if (!pRow) {
printf("Malloc failed. Out of memory.\n");
exit(1);
}
// Copy the row
memcpy(pRow, &(pIpForwardTab le->table[i]),
sizeof(MIB_IPFO RWARDROW));
}
// Delete the old default gateway entry.
dwStatus = myDeleteIpForwa rdEntry(&(pIpFo rwardTable->table[i]));
if (dwStatus != ERROR_SUCCESS) {
printf("Could not delete old gateway\n");
exit(1);
}
}
}
// Set the nexthop field to our new gateway - all the other properties
of the route will
// be the same as they were previously.
pRow->dwForwardNextH op = NewGateway;
// Create a new route entry for the default gateway.
dwStatus = myCreateIpForwa rdEntry(pRow);
if (dwStatus == NO_ERROR)
printf("Gateway changed successfully\n" );
else if (dwStatus == ERROR_INVALID_P ARAMETER)
printf("Invalid parameter.\n");
else
printf("Error: %d\n", dwStatus);
// Free resources
if (pIpForwardTabl e)
free(pIpForward Table);
if (pRow)
free(pRow);
}
#ifdef __cplusplus
}
#endif
#ifdef _MANAGED
#pragma managed(pop)
#endif