473,378 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Add C code to my C# application?

I'm developing for Windows Mobile 2005 PPC using Visual Studio 2005.

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/de...rwardtable.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?

Nov 22 '06 #1
8 6703

"cyberco" <cy*****@gmail.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...
I'm developing for Windows Mobile 2005 PPC using Visual Studio 2005.

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/de...rwardtable.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?
Try a New Project->Visual C++->CLR->Class Library. Then, from the C#
project references, choose Projects, and then the name of the C++ project.
Nov 22 '06 #2
cyberco wrote:
I'm developing for Windows Mobile 2005 PPC using Visual Studio 2005.

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/de...rwardtable.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?
See if this article helps -
http://bertjansen.spaces.live.com/Blog/cns!D9B33D4A940B6FFD!192.entry.
--
Tom Porterfield

Nov 22 '06 #3


I actually am having trouble with a similar issue. In order to
reference the DLL project the way just described, the C++ has to be in
cpp files and compiled with the /clr switch. Otherwise you get some
sort of "can't link with native dll" error with /clr, and without /clr
you can't compile as c code, you have to use *.cpp files compiled as
c++. This difference isn't significant unless you are doing really low
level stuff with your C that is not exceptable by the C++ compiler.

I think you can use native C dll's without the source code I believe,
and without referencing the project. There is an Import attribute that
you can use in C#. I don't think it is compile time safe though, as I
recall that you must give it the name of the DLL and functions you
intend to call. I haven't quite grasped how it makes sure all the
function calls into the DLL are passing the correct type of parameters.
I plan to read up on that more. Sorry I've not been more help.

Tom Porterfield wrote:
cyberco wrote:
I'm developing for Windows Mobile 2005 PPC using Visual Studio 2005.

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/de...rwardtable.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?

See if this article helps -
http://bertjansen.spaces.live.com/Blog/cns!D9B33D4A940B6FFD!192.entry.
--
Tom Porterfield
Nov 22 '06 #4
I think you can use native C dll's without the source code I believe,
and without referencing the project. There is an Import attribute that
you can use in C#. I don't think it is compile time safe though, as I
recall that you must give it the name of the DLL and functions you
intend to call. I haven't quite grasped how it makes sure all the
function calls into the DLL are passing the correct type of parameters.
I plan to read up on that more. Sorry I've not been more help.
The OP said he already tried p/invoke and it wasn't working out. Certainly
it's a lot easier to use the C++ compiler, because it understands the
original header file directly.

www.pinvoke.net is a good place to get the DllImport definitions so you
don't have to make them yourself.
Nov 22 '06 #5
>Try a New Project->Visual C++->CLR->Class Library. Then, from the C#
project references, choose Projects, and then the name of the C++ project.
This unfortunately doesn't work for device projects, I get the error:
"A reference to 'projectx' could not be added because it is not a
device project".
Is there no way to just compile the C++ code, create a dll and use that
in the C# project?

Nov 23 '06 #6
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:

---------------------------------------------------------------------------------------------
MyC# ==invokes==MyC++ ==invokes==existingDLL

---------------------------------------------------------------------------------------------
My problems are with using VS. What type(s) of project should I use?
How do I reference the existing DLL in my C++ code?

Nov 23 '06 #7
You need to include the iphlpapi.lib library file in your project.
This file defines the prototypes for the visible methods within the
dll. Then you can call the method directly from your code.

cyberco wrote:
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:

---------------------------------------------------------------------------------------------
MyC# ==invokes==MyC++ ==invokes==existingDLL

---------------------------------------------------------------------------------------------
My problems are with using VS. What type(s) of project should I use?
How do I reference the existing DLL in my C++ code?
Nov 24 '06 #8
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/...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 (*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[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_IPFORWARDROW)malloc(sizeof(MIB_IPFORWARDROW) );
if (!pRow) {
printf("Malloc failed. Out of memory.\n");
exit(1);
}
// Copy the row
memcpy(pRow, &(pIpForwardTable->table[i]),
sizeof(MIB_IPFORWARDROW));
}

// Delete the old default gateway entry.
dwStatus = myDeleteIpForwardEntry(&(pIpForwardTable->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->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

Nov 27 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kenneth W. Freo | last post by:
I have an ASP based application that in the default.asp sets a run at server variable of Application("Value") = 0 in Application_OnStart. Among other things in the Session_OnStart their is the...
3
by: Giovanni pepe | last post by:
Hello, in a VB.NET Application I had a problem after a Drag&Drop operation from Windows Desktop (Dataformats.filedrop) to listview.... In debug there are no errors so I've decided to implement the...
0
by: Chad Beckner | last post by:
Hi everyone, I am developing a "common repository" of methods which can be imported into Windows and Web forms. However, I am having some troubles which I need your help with! 1. Every...
1
by: Reny J Joseph Thuthikattu | last post by:
Hi, I made an application on vb.net.Before running the application i want to chack the istance of that application is already running? If so i need to abort the instance.Can any one tell me how...
5
by: Unforgiven | last post by:
I downloaded a web timer control from http://www.eggheadcafe.com/articles/20021006.asp and it's written in C#. The code and .DLL comes with the download so I'm trying to convert the sample which...
3
by: RobKaratzas | last post by:
hi Folk this is frustrating as heck... 1. new keys are only showing 'Nothing' as their value... 2. VS.NET: rebuilding everything, restarted IIS, no bad XML, ... 3. old keys are still showing...
2
by: Alan Foxmore | last post by:
Hi Everyone, I'm new to ASP.NET so maybe this is easy. I'm using ASP.NET 2.0. I'm finding that the Application object (the HttpApplicationState object) is allowing me to add the same key...
1
by: Daniel | last post by:
Hallo, I'd like to hide application in Pocket PC. My application is written in C# 2.0 for WM 5.0. I have some questions: 1. When it starts in takes about 5-6 seconds. The whole application...
6
by: =?Utf-8?B?ZmFpcnl2b2ljZQ==?= | last post by:
abviously you need to intall the .net framework to have a .net application run, but is there anyway i can change .net application into native code application so that it can run directly without...
3
by: qwedster | last post by:
Hello! I tried to use Application State as given in MSDN (http://msdn.microsoft.com/en-us/library/ms178594.aspx): I opened a WebApplicaion (C#) in Visual Studio 2008 and added Global.asax file....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.