473,770 Members | 1,833 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6735

"cyberco" <cy*****@gmail. comwrote in message
news:11******** *************@m 73g2000cwd.goog legroups.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.spac es.live.com/Blog/cns!D9B33D4A940 B6FFD!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.spac es.live.com/Blog/cns!D9B33D4A940 B6FFD!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==exis tingDLL

---------------------------------------------------------------------------------------------
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==exis tingDLL

---------------------------------------------------------------------------------------------
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 '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

Nov 27 '06 #9

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

Similar topics

1
1817
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 code Application("Value")=CInt (Application("Value"))+1. I have two Workstations running Windows XP Pro that when the the default.asp is run (IE6) this variable is incremented not by one but two or more. The Session_OnEnd is supposed to decrement...
3
1528
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 application ThreadException event (throug addhandler in sub main) After this the problem was disappeared... Why? Thank You
0
1036
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 property/function etc. has to be setup as Public Shared in the common repository, or else the "main" application can't see the imported functions (i.e. "Imports Common.Utilities"). I don't want "others" to get a hold of this library and use it in...
1
1212
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 can i do that? Reny
5
1483
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 was written in C# to VB (ASP.NET). Here is the following code in the Default.aspx page... private void PgTimer1_Elapsed(object sender, System.EventArgs e) { if (Application == null) {
3
1261
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 values (and can change and is reflected). 4.another developer just looked at this with me and we're looking at each other just wondering what the heck is going on. 5. this is web.config at the virt. dir level.
2
1777
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 multiple times. The easiest way to describe the problem is to just show you. In my Page_Load() I have this code:
1
1879
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 consists of 5-6 forms so it's rather small. Is it possible to speed it up? Or is it normal time for C# application?
6
1418
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 the .net framework? it seems dosen't make any sence, but for some reason i have to do in this way if possible.
3
3297
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. And added the following code (from MSDN) in Global.asax: <%@ Application Codebehind="Global.asax.cs" Inherits="WebApplication1.Global" Language="C#" %>
0
9617
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10257
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10099
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10037
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8931
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.