Adding C code to my C# application

C

cyberco

I need to invoke methods on a DLL (Iphlpapi.dll) but I can't get this
working from C# using p/invoke so I want to write some C code that
invokes the DLL. Similar to this:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/iphlp/iphlp/getipforwardtable.asp

Now how can I, in Visual Studio 2005, create and use such a C class ?
Do I have to create an 'MFC Smart Device DLL' project? If so, how do I
combine it with my C# project?

I'm developing for Windows Mobile 2005 PPC.
 
P

Paul G. Tobey [eMVP]

Don't do that. MFC DLLs assume that they're running in the context of a MFC
application. You can wrap your other calls with some standard C functions,
but you're just making more problems for youself...

Paul T.
 
G

Guest

Create either a DLL or an EXE - do *not* use MFC. Then add a method (not a
class) to call what you want.
 
G

Guest

And one last thing, you can't "add" it to a C# app. You can P/Invoke it,
but you'd have to make it a separate C DLL with publicly exported,
non-decorated symbols.
 
C

cyberco

I'm a newbie when it comes to creating C++ applications (for Windows
Mobile) using Visual Studio 2005. Now I'm stuck trying to invoke an
existing DLL (Iphlpapi.dll, which is a common DLL on windows) from my
own C++ class which in turn is invoked from C#: Schematic:
 
C

cyberco

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 'createIpForwardEntry()'
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...72c61c3b22b/369e27fc9415d06e#369e27fc9415d06e
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 (*getIpForwardTableFunction)(PMIB_IPFORWARDTABLE, PULONG,
BOOL);
typedef DWORD (*createIpForwardEntryFunction)(PMIB_IPFORWARDROW);
typedef DWORD (*setIpForwardEntryFunction)(PMIB_IPFORWARDROW);
typedef DWORD (*deleteIpForwardEntryFunction)(PMIB_IPFORWARDROW);

HINSTANCE hinstLib;
getIpForwardTableFunction myGetIpForwardTable = NULL;
createIpForwardEntryFunction myCreateIpForwardEntry = NULL;
setIpForwardEntryFunction mySetIpForwardEntry = NULL;
deleteIpForwardEntryFunction myDeleteIpForwardEntry = NULL;

BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
) {
LPCWSTR dllName = LPCWSTR("Iphlpapi.dll");
hinstLib = LoadLibrary(dllName);
if (hinstLib == NULL) {
printf("Error: could not load Iphlpapi.dll\n");
return FALSE;
}

myGetIpForwardTable = (getIpForwardTableFunction)
GetProcAddress(hinstLib, "GetIpForwardTable");
if (myGetIpForwardTable == NULL) {
printf("Error: could not find function getipforwardtable");
return FALSE;
}

myCreateIpForwardEntry = (createIpForwardEntryFunction)
GetProcAddress(hinstLib, "CreateIpForwardEntry");
if (myCreateIpForwardEntry == NULL) {
printf("Error: could not find function createipforwardentry");
return FALSE;
}

mySetIpForwardEntry = (setIpForwardEntryFunction)
GetProcAddress(hinstLib, "SetIpForwardEntry");
if (mySetIpForwardEntry == NULL) {
printf("Error: could not find function setipforwardentry");
return FALSE;
}

myDeleteIpForwardEntry = (deleteIpForwardEntryFunction)
GetProcAddress(hinstLib, "DeleteIpForwardEntry");
if (myDeleteIpForwardEntry == NULL) {
printf("Error: could not find function deleteipforwardentry");
return FALSE;
}

return TRUE;
}

__declspec(dllexport) void __stdcall createIpForwardEntry() {
PMIB_IPFORWARDTABLE pIpForwardTable = NULL;
PMIB_IPFORWARDROW 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 = myGetIpForwardTable(pIpForwardTable, &dwSize, bOrder);
if (dwStatus == ERROR_INSUFFICIENT_BUFFER) {
// Allocate the memory for the table
if (!(pIpForwardTable = (PMIB_IPFORWARDTABLE)malloc(dwSize))) {
printf("Malloc failed. Out of memory.\n");
exit(1);
}
// Now get the table.
dwStatus = myGetIpForwardTable(pIpForwardTable, &dwSize, bOrder);
}

if (dwStatus != ERROR_SUCCESS) {
printf("getIpForwardTable failed.\n");
if (pIpForwardTable)
free(pIpForwardTable);
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 (pIpForwardTable->table.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_IPFORWARDROW)malloc(sizeof(MIB_IPFORWARDROW));
if (!pRow) {
printf("Malloc failed. Out of memory.\n");
exit(1);
}
// Copy the row
memcpy(pRow, &(pIpForwardTable->table),
sizeof(MIB_IPFORWARDROW));
}

// Delete the old default gateway entry.
dwStatus = myDeleteIpForwardEntry(&(pIpForwardTable->table));

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->dwForwardNextHop = NewGateway;

// Create a new route entry for the default gateway.
dwStatus = myCreateIpForwardEntry(pRow);

if (dwStatus == NO_ERROR)
printf("Gateway changed successfully\n");
else if (dwStatus == ERROR_INVALID_PARAMETER)
printf("Invalid parameter.\n");
else
printf("Error: %d\n", dwStatus);

// Free resources
if (pIpForwardTable)
free(pIpForwardTable);
if (pRow)
free(pRow);
}


#ifdef __cplusplus
}
#endif

#ifdef _MANAGED
#pragma managed(pop)
#endif
 
G

Guest

I doubt there are many who actually have done this. I've spent the last 4
weeks working largely in WZC and IPHLPAPI wrapping them in C# , and I can
say it's a gruelling process. I've not hit the routing and ARP stuff yet,
but I likely will by the end of the week. So I've very familiar with what
you're trying to do (and if you've got interest in betaing the product when
we're done send me an email offline).

Just glancing at the APIs you're trying to use, it should be pretty simple -
there are no interened pointers or ugliness like that, so it should be very
straightforward. I didn't walk your structs carefully, but the call to
CreateIpForwardEntry looks right (and would be hard to get wrong if the
struct is declared right). Don't worry about int or uint - it's all the
same to the marshaler. The MarshalAs stuff is also superfluous chaff in
this case - get rid of it so it's readable.

My guess is that something actually in the struct is what it doesn't like.
The -1's for the metrics set off a red flag to me - but I assume you've
looked at RFC 1354 and know that it's a correct and valid value there.

Where is the interface index (ifIndex) you're setting coming from? That
being wrong would cause a problem (though I usually see an error code 2 for
that).


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--



cyberco said:
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 'createIpForwardEntry()'
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...72c61c3b22b/369e27fc9415d06e#369e27fc9415d06e
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 (*getIpForwardTableFunction)(PMIB_IPFORWARDTABLE, PULONG,
BOOL);
typedef DWORD (*createIpForwardEntryFunction)(PMIB_IPFORWARDROW);
typedef DWORD (*setIpForwardEntryFunction)(PMIB_IPFORWARDROW);
typedef DWORD (*deleteIpForwardEntryFunction)(PMIB_IPFORWARDROW);

HINSTANCE hinstLib;
getIpForwardTableFunction myGetIpForwardTable = NULL;
createIpForwardEntryFunction myCreateIpForwardEntry = NULL;
setIpForwardEntryFunction mySetIpForwardEntry = NULL;
deleteIpForwardEntryFunction myDeleteIpForwardEntry = NULL;

BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
) {
LPCWSTR dllName = LPCWSTR("Iphlpapi.dll");
hinstLib = LoadLibrary(dllName);
if (hinstLib == NULL) {
printf("Error: could not load Iphlpapi.dll\n");
return FALSE;
}

myGetIpForwardTable = (getIpForwardTableFunction)
GetProcAddress(hinstLib, "GetIpForwardTable");
if (myGetIpForwardTable == NULL) {
printf("Error: could not find function getipforwardtable");
return FALSE;
}

myCreateIpForwardEntry = (createIpForwardEntryFunction)
GetProcAddress(hinstLib, "CreateIpForwardEntry");
if (myCreateIpForwardEntry == NULL) {
printf("Error: could not find function createipforwardentry");
return FALSE;
}

mySetIpForwardEntry = (setIpForwardEntryFunction)
GetProcAddress(hinstLib, "SetIpForwardEntry");
if (mySetIpForwardEntry == NULL) {
printf("Error: could not find function setipforwardentry");
return FALSE;
}

myDeleteIpForwardEntry = (deleteIpForwardEntryFunction)
GetProcAddress(hinstLib, "DeleteIpForwardEntry");
if (myDeleteIpForwardEntry == NULL) {
printf("Error: could not find function deleteipforwardentry");
return FALSE;
}

return TRUE;
}

__declspec(dllexport) void __stdcall createIpForwardEntry() {
PMIB_IPFORWARDTABLE pIpForwardTable = NULL;
PMIB_IPFORWARDROW 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 = myGetIpForwardTable(pIpForwardTable, &dwSize, bOrder);
if (dwStatus == ERROR_INSUFFICIENT_BUFFER) {
// Allocate the memory for the table
if (!(pIpForwardTable = (PMIB_IPFORWARDTABLE)malloc(dwSize))) {
printf("Malloc failed. Out of memory.\n");
exit(1);
}
// Now get the table.
dwStatus = myGetIpForwardTable(pIpForwardTable, &dwSize, bOrder);
}

if (dwStatus != ERROR_SUCCESS) {
printf("getIpForwardTable failed.\n");
if (pIpForwardTable)
free(pIpForwardTable);
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 (pIpForwardTable->table.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_IPFORWARDROW)malloc(sizeof(MIB_IPFORWARDROW));
if (!pRow) {
printf("Malloc failed. Out of memory.\n");
exit(1);
}
// Copy the row
memcpy(pRow, &(pIpForwardTable->table),
sizeof(MIB_IPFORWARDROW));
}

// Delete the old default gateway entry.
dwStatus = myDeleteIpForwardEntry(&(pIpForwardTable->table));

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->dwForwardNextHop = NewGateway;

// Create a new route entry for the default gateway.
dwStatus = myCreateIpForwardEntry(pRow);

if (dwStatus == NO_ERROR)
printf("Gateway changed successfully\n");
else if (dwStatus == ERROR_INVALID_PARAMETER)
printf("Invalid parameter.\n");
else
printf("Error: %d\n", dwStatus);

// Free resources
if (pIpForwardTable)
free(pIpForwardTable);
if (pRow)
free(pRow);
}


#ifdef __cplusplus
}
#endif

#ifdef _MANAGED
#pragma managed(pop)
#endif
 
C

cyberco

So I've very familiar with what
you're trying to do (and if you've got interest in betaing the product when
we're done send me an email offline).

I would be very interested. I'l email you offline.
My guess is that something actually in the struct is what it doesn't like.
The -1's for the metrics set off a red flag to me - but I assume you've
looked at RFC 1354 and know that it's a correct and valid value there.

Yes, I've triple checked the values from RFC1354, just to be sure, but
it doesn't make any difference.
Where is the interface index (ifIndex) you're setting coming from? That
being wrong would cause a problem (though I usually see an error code 2 for
that).

The interface index (ifindex) I retrieve at runtime and check them by
hand as well.

I've now managed to set up VS2005 that I create my own DLL which I can
invoke from C#. That's one step. Unfortunately invoking the
IpHlpApi.dll from my own DLL is still not working. Somehow this doesn't
work:

==============
LPCWSTR dllName = LPCWSTR("Iphlpapi.dll");
hinstLib = LoadLibrary(dllName);
if (hinstLib == NULL) {
printf("Error: could not load Iphlpapi.dll\n");
return FALSE;
}
==============
 
G

Guest

Statically bind to the lib instead of trying to dynamically load it. Much
easier in the short term to get it working.

Also, your definition of the DLL name looks wrong to me. I think the code
should look more like this:

#define IPHLPDLL _T("iphlpapi.dll")
....
HINSTANCE hLib = LoadLibrary(IPHLPDLL);

if(! hLib) { // failed }
 
C

cyberco

Thanks, you were right about the name.
I'm now at the stage that I can use my own DLL which in turn invokes
'iphlpapi.dll': See code below.
Unfortunately the following method STILL gives me error 87 (invalid
param).

dwStatus = myCreateIpForwardEntry(pRow);

I would say that specifying the gateway (the only part of the copied
IpForwardRow that is changed) as follows should work:

DWORD NewGateway = 0x0100007F;

Unfortunately..... Since I STILL get this error (both from my C# and my
C code) I suspect that it is impossible to change the Route Table on
Windows Mobile....

What do you think?



===== MY DLL ==========
#include "stdafx.h"
#include <windows.h>
#include <commctrl.h>
#include <stdio.h>
#include <stdlib.h>
#include <iphlpapi.h>
#include <winerror.h>

#ifdef _MANAGED
#pragma managed(push, off)
#endif
#ifdef __cplusplus
extern "C" {
#endif

typedef DWORD (*getIpForwardTableFunction)(PMIB_IPFORWARDTABLE, PULONG,
BOOL);
typedef DWORD (*createIpForwardEntryFunction)(PMIB_IPFORWARDROW);
typedef DWORD (*setIpForwardEntryFunction)(PMIB_IPFORWARDROW);
typedef DWORD (*deleteIpForwardEntryFunction)(PMIB_IPFORWARDROW);

HINSTANCE hinstLib;
#define IPHLPDLL _T("iphlpapi.dll")
getIpForwardTableFunction myGetIpForwardTable = NULL;
createIpForwardEntryFunction myCreateIpForwardEntry = NULL;
setIpForwardEntryFunction mySetIpForwardEntry = NULL;
deleteIpForwardEntryFunction myDeleteIpForwardEntry = NULL;

BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
) {

hinstLib = LoadLibrary(IPHLPDLL);
if (!hinstLib) {
printf("Error: could not load IpHlpAPI.dll\n");
return FALSE;
}

myCreateIpForwardEntry = (createIpForwardEntryFunction)
GetProcAddress(hinstLib, _T("CreateIpForwardEntry"));
if (myCreateIpForwardEntry == NULL) {
printf("Error: could not find function createipforwardentry");
return FALSE;
}

myGetIpForwardTable = (getIpForwardTableFunction)
GetProcAddress(hinstLib, _T("GetIpForwardTable"));
if (myGetIpForwardTable == NULL) {
printf("Error: could not find function getipforwardtable");
return FALSE;
}

mySetIpForwardEntry = (setIpForwardEntryFunction)
GetProcAddress(hinstLib, _T("SetIpForwardEntry"));
if (mySetIpForwardEntry == NULL) {
printf("Error: could not find function setipforwardentry");
return FALSE;
}

myDeleteIpForwardEntry = (deleteIpForwardEntryFunction)
GetProcAddress(hinstLib, _T("DeleteIpForwardEntry"));
if (myDeleteIpForwardEntry == NULL) {
printf("Error: could not find function deleteipforwardentry");
return FALSE;
}


return TRUE;
}

extern "C" _declspec(dllexport) WORD WINAPI testMe(WORD InVal) {
return InVal + 2;
}

extern "C" _declspec(dllexport) DWORD WINAPI createIpForwardEntry() {
PMIB_IPFORWARDTABLE pIpForwardTable = NULL;
PMIB_IPFORWARDROW 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
DWORD NewGateway = 0x0100007F;

// Find out how big our buffer needs to be.
dwStatus = myGetIpForwardTable(pIpForwardTable, &dwSize, bOrder);
if (dwStatus == ERROR_INSUFFICIENT_BUFFER) {
// Allocate the memory for the table
if (!(pIpForwardTable = (PMIB_IPFORWARDTABLE)malloc(dwSize))) {
printf("Malloc failed. Out of memory.\n");
exit(1);
}
// Now get the table
dwStatus = myGetIpForwardTable(pIpForwardTable, &dwSize, bOrder);
}

if (dwStatus != ERROR_SUCCESS) {
printf("getIpForwardTable failed.\n");
if (pIpForwardTable)
free(pIpForwardTable);
exit(1);
}

pRow = (PMIB_IPFORWARDROW)malloc(sizeof(MIB_IPFORWARDROW));
if (!pRow) {
printf("Malloc failed. Out of memory.\n");
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++) {
memcpy(pRow, &(pIpForwardTable->table), sizeof(MIB_IPFORWARDROW));
//if (pIpForwardTable->table.dwForwardDest == 0) {
if (pIpForwardTable->table.dwForwardDest == 0xFFFFFFFF) {
dwStatus = myDeleteIpForwardEntry(&(pIpForwardTable->table));

if (dwStatus != ERROR_SUCCESS) {
printf("Could not delete old gateway\n");
dwStatus = 1;
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->dwForwardNextHop = NewGateway;

// Create a new route entry for the default gateway.
dwStatus = myCreateIpForwardEntry(pRow);

if (dwStatus == NO_ERROR)
printf("Gateway changed successfully\n");
else if (dwStatus == ERROR_INVALID_PARAMETER)
printf("Invalid parameter.\n");
else
printf("Error: %d\n", dwStatus);

// Free resources
if (pIpForwardTable)
free(pIpForwardTable);
if (pRow)
free(pRow);
return dwStatus;
}

#ifdef __cplusplus
}
#endif

#ifdef _MANAGED
#pragma managed(pop)
#endif

==================
 
G

Guest

I've finally got that code done, so I will be able to see if I can alter the
table tomorrow. Offhand I'd expect a different error if it wasn't
supported.


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--


cyberco said:
Thanks, you were right about the name.
I'm now at the stage that I can use my own DLL which in turn invokes
'iphlpapi.dll': See code below.
Unfortunately the following method STILL gives me error 87 (invalid
param).

dwStatus = myCreateIpForwardEntry(pRow);

I would say that specifying the gateway (the only part of the copied
IpForwardRow that is changed) as follows should work:

DWORD NewGateway = 0x0100007F;

Unfortunately..... Since I STILL get this error (both from my C# and my
C code) I suspect that it is impossible to change the Route Table on
Windows Mobile....

What do you think?



===== MY DLL ==========
#include "stdafx.h"
#include <windows.h>
#include <commctrl.h>
#include <stdio.h>
#include <stdlib.h>
#include <iphlpapi.h>
#include <winerror.h>

#ifdef _MANAGED
#pragma managed(push, off)
#endif
#ifdef __cplusplus
extern "C" {
#endif

typedef DWORD (*getIpForwardTableFunction)(PMIB_IPFORWARDTABLE, PULONG,
BOOL);
typedef DWORD (*createIpForwardEntryFunction)(PMIB_IPFORWARDROW);
typedef DWORD (*setIpForwardEntryFunction)(PMIB_IPFORWARDROW);
typedef DWORD (*deleteIpForwardEntryFunction)(PMIB_IPFORWARDROW);

HINSTANCE hinstLib;
#define IPHLPDLL _T("iphlpapi.dll")
getIpForwardTableFunction myGetIpForwardTable = NULL;
createIpForwardEntryFunction myCreateIpForwardEntry = NULL;
setIpForwardEntryFunction mySetIpForwardEntry = NULL;
deleteIpForwardEntryFunction myDeleteIpForwardEntry = NULL;

BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
) {

hinstLib = LoadLibrary(IPHLPDLL);
if (!hinstLib) {
printf("Error: could not load IpHlpAPI.dll\n");
return FALSE;
}

myCreateIpForwardEntry = (createIpForwardEntryFunction)
GetProcAddress(hinstLib, _T("CreateIpForwardEntry"));
if (myCreateIpForwardEntry == NULL) {
printf("Error: could not find function createipforwardentry");
return FALSE;
}

myGetIpForwardTable = (getIpForwardTableFunction)
GetProcAddress(hinstLib, _T("GetIpForwardTable"));
if (myGetIpForwardTable == NULL) {
printf("Error: could not find function getipforwardtable");
return FALSE;
}

mySetIpForwardEntry = (setIpForwardEntryFunction)
GetProcAddress(hinstLib, _T("SetIpForwardEntry"));
if (mySetIpForwardEntry == NULL) {
printf("Error: could not find function setipforwardentry");
return FALSE;
}

myDeleteIpForwardEntry = (deleteIpForwardEntryFunction)
GetProcAddress(hinstLib, _T("DeleteIpForwardEntry"));
if (myDeleteIpForwardEntry == NULL) {
printf("Error: could not find function deleteipforwardentry");
return FALSE;
}


return TRUE;
}

extern "C" _declspec(dllexport) WORD WINAPI testMe(WORD InVal) {
return InVal + 2;
}

extern "C" _declspec(dllexport) DWORD WINAPI createIpForwardEntry() {
PMIB_IPFORWARDTABLE pIpForwardTable = NULL;
PMIB_IPFORWARDROW 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
DWORD NewGateway = 0x0100007F;

// Find out how big our buffer needs to be.
dwStatus = myGetIpForwardTable(pIpForwardTable, &dwSize, bOrder);
if (dwStatus == ERROR_INSUFFICIENT_BUFFER) {
// Allocate the memory for the table
if (!(pIpForwardTable = (PMIB_IPFORWARDTABLE)malloc(dwSize))) {
printf("Malloc failed. Out of memory.\n");
exit(1);
}
// Now get the table
dwStatus = myGetIpForwardTable(pIpForwardTable, &dwSize, bOrder);
}

if (dwStatus != ERROR_SUCCESS) {
printf("getIpForwardTable failed.\n");
if (pIpForwardTable)
free(pIpForwardTable);
exit(1);
}

pRow = (PMIB_IPFORWARDROW)malloc(sizeof(MIB_IPFORWARDROW));
if (!pRow) {
printf("Malloc failed. Out of memory.\n");
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++) {
memcpy(pRow, &(pIpForwardTable->table), sizeof(MIB_IPFORWARDROW));
//if (pIpForwardTable->table.dwForwardDest == 0) {
if (pIpForwardTable->table.dwForwardDest == 0xFFFFFFFF) {
dwStatus = myDeleteIpForwardEntry(&(pIpForwardTable->table));

if (dwStatus != ERROR_SUCCESS) {
printf("Could not delete old gateway\n");
dwStatus = 1;
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->dwForwardNextHop = NewGateway;

// Create a new route entry for the default gateway.
dwStatus = myCreateIpForwardEntry(pRow);

if (dwStatus == NO_ERROR)
printf("Gateway changed successfully\n");
else if (dwStatus == ERROR_INVALID_PARAMETER)
printf("Invalid parameter.\n");
else
printf("Error: %d\n", dwStatus);

// Free resources
if (pIpForwardTable)
free(pIpForwardTable);
if (pRow)
free(pRow);
return dwStatus;
}

#ifdef __cplusplus
}
#endif

#ifdef _MANAGED
#pragma managed(pop)
#endif

==================
 
C

cyberco

Hi Chris,

Did you ever succeed in altering the route table? I would be very
interested if you did.
I returned to the problem after a short break, but I still haven't
solved it. :)

Cheers,
CyBerco
 
G

Guest

I did, though it seems a bit picky aboutwhich entries you can alter. I'm
not networking and routing expert, so I assume that it's only going to allow
you to add routes that are valid. So I've been able to remove and add to
some degree, but when it doesn't like what I'm trying to add I get the
invalid paramter failure.

We'll be announcing a beta for the library shortly.
 
C

cyberco

Wow, great. Any chance you will share the solution here, or do we have
to wait for your library?
Was I on the right track, or was I 'barking up the wrong tree'? :)
 
G

Guest

The solution is way more complex that I'm willing to post. IIRC you were
probably on the right track, but I really didn't look much at what you did
while I implemented it. I just kep with the design style I'd used for the
rest of the library, so lots and lots of unsafe code.


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--


cyberco said:
Wow, great. Any chance you will share the solution here, or do we have
to wait for your library?
Was I on the right track, or was I 'barking up the wrong tree'? :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top