|
Hi!
I need to use GPC library in a C# project.
GPC library is written in pure C and is available on http://www.cs.man.ac.uk/aig/staff/alan/software/
So what I did is I worte a Win32 .dll with exports of the library functions:
extern "C"
{
__declspec(dllexport) void gpc_add_contour(gpc_polygon *polygon, gpc_vertex_list *contour, int hole);
__declspec(dllexport) void gpc_polygon_clip(gpc_op set_operation, gpc_polygon *subject_polygon, gpc_polygon *clip_polygon, gpc_polygon *result_polygon);
....etc...
}
So I was thinking...ok...now I have the .dll and now I can use C# attribute DllImport to import functions from .dll.
But as you can see there are structures that pass as pointers to the functions. I don't know how to marshal it/make it usable in C#.
This is my current progress in implementing the port of GPC to C# (I don't know when to use IntPtr or something else):
using System;
using System.Text;
using System.Runtime.InteropServices;
namespace nsGPC
{
public class GPC
{
public enum gpc_op
{
GPC_DIFF,
GPC_INT,
GPC_XOR,
GPC_UNION
}
[StructLayout(LayoutKind.Sequential)]
public class gpc_vertex
{
public double x;
public double y;
}
[StructLayout(LayoutKind.Sequential)]
public class gpc_vertex_list
{
public int num_vertices;
public IntPtr vertex; //pointer to gpc_vertex
}
[StructLayout(LayoutKind.Sequential)]
public class gpc_polygon
{
public int num_contours;
public IntPtr hole; //pointer to int
public IntPtr contour; //pointer to gpc_vertex_list
}
[StructLayout(LayoutKind.Sequential)]
public class gpc_tristrip
{
public int num_strips;
public IntPtr strip; //pointer to gpc_vertex_list
}
[DllImport("GPC.NET.dll", CharSet=CharSet.Ansi)]
public static extern void gpc_add_contour(ref gpc_polygon polygon, ref gpc_vertex_list contour, int hole);
[DllImport("GPC.NET.dll", CharSet=CharSet.Ansi)]
public static extern void gpc_polygon_clip(gpc_op set_operation, ref gpc_polygon subject_polygon, ref gpc_polygon clip_polygon, ref gpc_polygon result_polygon);
[DllImport("GPC.NET.dll", CharSet=CharSet.Ansi)]
public static extern void gpc_free_polygon(ref gpc_polygon polygon);
}
}
Well, anyways...Is anyone here with enough knowledge of porting this library to C#?
MP.
|
|
|
|
|
Since it's a C library, I imagine it has lots of functions. I believe that it would be better write a managed C++ class that calls the C library natively, otherwise you'll lose lots of time writing interop code that MC++ can create automatically for you (it's called IJW). After this, you just add a reference from C# and call it as any managed class.
Due to technical difficulties my previous signature, "I see dumb people" will be off until further notice. Too many people were thinking I was talking about them...
|
|
|
|
|
Yeah, but I have to agree with Daniel on this one. P/Invoke is great when you have to call a few exported functions, but many times you run into a lot of problems, especially with lots of exports.
Say one of these exported functions uses nested structs. The marshellers used by the CLR does not support nested structs (and the .NET Compact Framework doesn't even support marshalling a struct at all). All this is easy to accomplish using a mixed-mode MC++ wrapper, exposing classes as a managed class (using the __gc extension, at least until .NET 2.0 is released) and using the native APIs from the native library you're trying to wrap.
I do a lot of interop (both COM and P/Invoke) for fun and I typically choose this route when I need to use a lot of exported functions or more complex functions with nested structs and the like. For an example, see my article, Using XML Digital Signatures for Application Licensing[^] where I use a MC++ assembly that uses a lot of the LSA APIs and exposes that as a managed class I can use with any other ".NET language".
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Cool.
Well, I forgot to mention, that this is my first .net project, so I'm kinda lost.
But, I managed to do a wrapper in MC++ and it's working great.
I needed to export only 2 C functions to .net and not so many as you two tought.
Anyways, I'm going to do it for all cca. 10 functions and publish it on the web as .NET port of GPC.
Thanks to both of you for directing me at the right way to solve the problem.
MP.
|
|
|
|
|
Hello,
I've searched IRC for a good C# channel but most of them I don't even get an answer, and believe me, I've waited for it. I can ask a question then leave and come back after a hour and still no answer.
Does Codeproject have an own irc channel?
I think it's a great way of discussing articles here on codeproject or helping each other.
Maybe we should start one if there is none?
Mybae we should not? =)
Anyway, have a good day! =)
~~~~~~~~~~~~~~
Martin Lundberg
Student, Sweden
I have to thank every member of the Code Project for making it such a great place for a beginner to learn!
|
|
|
|
|
Martin Lundberg wrote:
I think it's a great way of discussing articles here on codeproject or helping each other.
Yeah, not like these forums or the message boards at the bottom of every article, huh?
No, CP does not have it's own IRC channel. That's what the forums are for.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hello agagin!
Well yea they are great but it's still not the same thing. It's not real time discussions. The discussions are often slow and sometimes even forgotten.
But I see you didn't like what I said and maybe you are right, a IRC channel would maybe just make the activity of the site to slow down, and thats not a good thing.
but still, If you need help IRC is very good (if they would ever answer =P)
~~~~~~~~~~~~~~
Martin Lundberg
Student, Sweden
I have to thank every member of the Code Project for making it such a great place for a beginner to learn!
|
|
|
|
|
If you opt to receive notification messages when you're replied to, they aren't lost. And unlike IRC, all threads are archived and accessible for several years here on CodeProject. If you need real-time communicae, then it means you're not planning ahead researching your project before you start. If you need answers immediately, you didn't plan ahead.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Bah! =P
It's still think it's nice to talk to a person in real time, discussing program related things =)
I'm not saying anything is bad here, I love this site =)
~~~~~~~~~~~~~~
Martin Lundberg
Student, Sweden
I have to thank every member of the Code Project for making it such a great place for a beginner to learn!
|
|
|
|
|
|
Word?
~~~~~~~~~~~~~~
Martin Lundberg
Student, Sweden
I have to thank every member of the Code Project for making it such a great place for a beginner to learn!
|
|
|
|
|
|
Hi,
I'm have problem with api functions in C#.I want to call API function to retreive listview information such as itemtext, item position... I use function SendMessage to get it but it doesn't work.
//API function :
//#define TreeView_GetItem(hwnd, pitem) \
// (BOOL)SNDMSG((hwnd), TVM_GETITEM, 0, (LPARAM)(TV_ITEM *)(pitem))
and my code in C# is :
public const int LVM_GETITEM =(LVM_FIRST + 75);//unicode
[DllImport("user32", CharSet = CharSet.Auto)]
public extern static int SendMessage(
IntPtr hWnd,
int wMsg,
long wParam,
ref LVITEM lParam);
//with LVITEM is the struct of ITEM in ListView :
unsafe struct LVITEM
{
public uint mask;
public int iItem;
public int iSubItem;
public uint state;
public uint stateMask;
public string pszText;
public int cchTextMax;
public int iImage;
public long lParam;
//public int iIndent;
public int iGroupId;
public uint cColumns; // tile view columns
public uint* puColumns;
} ;
Please show me anything wrong in my struct LVITEM ???
Thank you.
NTHEVU
|
|
|
|
|
Why are you even P/Invoking anything? What you want is already possible using the ListView class and the ListViewItem class. The ListViewItem.Text property gets the text of the ListViewItem , which you can get by enumerating through the ListView.Items or using ListView.SelectedItems or something similar. If you want a particular item's region, then you can use ListView.GetItemRect , which can retrieve information about several parts of a ListViewItem .
Besides, you don't need an unsafe context and your SendMessage declaration is wrong. The wParam should be an IntPtr since it will be 32 bits on a 32-bit OS and 64 bits on a 64-bit OS (which is dependent on the processor). The last member of your struct should also be just an IntPtr .
Seriously, though, don't waste your time doing this. Everything you're trying to do according to your post is easy using what's provided in the .NET FCL (namely, the System.Windows.Forms.dll assembly).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi and thank you for your answer. I know C# have class ListView with many function to manage my code but i want to use API function to retrieve listview information form the difference application. So i choose API, because i don't know how to retreive listview information in other application. So if you experience in this case, please help me.
Ah, IntPtr type have error when i try to convert or cast to String or other common Type in API structture such as LVITEM, LVFINDINFO ...
NTHEVU
|
|
|
|
|
You can't simply cast a string to and from an IntPtr . You need to use the Marshal class methods, such as Marshal.PtrToStringAuto . If you're looking for these signatures and not sure what you're doing, I recommend you take a look at http://pinvoke.net[^].
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thank you so much, i learn much from your instruction
thankx, NTHEVU
|
|
|
|
|
I wanna know, wich files i have to upload.
Only the Server.asmx? or Server.dll ?
or Server.asmx and Server.asmx.cs...
and how i use this DLL?
Thanks.
|
|
|
|
|
If you want to modify the code, you'll need the Server.asmx.cs, if you just want to reference the dll in a project, then use Server.dll.
Could you be more specific in what you are trying to do? The question is a bit vague.
R.Bischoff
.NET, Kommst du mit?
|
|
|
|
|
If you're doing this in VS.NET, just click on the Copy Project button at the top of the Solution Explorer when your project is selected. It asks which you want to copy. Typically in a deployment scenario you just copy files necessary for the application to run. This would be the Server.asmx file and the Server.dll file, which either goes into the bin sub-directory of the application root directory (/, /myapp, whatever) or the Global Assembly Cache (GAC) making it easily accessible for web applications that require it and easy to version (since the GAC stores all versions of an assembly that you install in it, unless of course you were to remove certain versions).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi all,
It's my first time to join this site. I having a problem with my program. It deals with huge amout of input. My input size is 15 MB which causes a low in virtual memory...
The program is about to search for a certain pattern in this input text. I'm using the Boyer moore pattern matching algorithm, so i have to load the whole file when started.
Is there a way or an algorithm to divide this file or something?
Thank you all.
yomna
|
|
|
|
|
If the algorithm requires that the whole file be read in (and frankly, I've never seen such an algorithm but it's true that I'm not familiar with the Boyer-Moore pattern), then you should find a new algorithm. Still, though, it may work if you buffer your file in a particular way.
Buffering the file reads in blocks at a time (commonly 4K, but it can be anything). If the algorithm allows for partial matches, for example, then you know you should keep the previous buffer (or at least part of it), read-in the next buffer, and join the two together to complete the entire match (if it does match).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
|
I want to sign my application and the assemblies that it use, so I know it has not been tampered with when it runs
Right now there are a binary crack for my application, and I thought I might make it a bit harder for the crackers by signing my assemblies...
So I read in MSDN about signing my assemblies with a strong name, but then I read that all the assemblies that the application calls need to be signed with the same private key...
This is a problem as I'm exposing a plug-in interface where people can make plug-in that the application loads at runtime.
If my program is signed with a strong name, does that mean that the plug-in's need to be signed with the same private key?
If it does it makes the whole strong-name thingy useless for me, as no one can develop plug-in's then...
Anyone out there that know more about this than me who can fill me in on it?
- Anders
Money talks, but all mine ever says is "Goodbye!"
ShotKeeper, my Photo Album / Organizer Application[^]My Photos[^]
|
|
|
|
|
But when a user make a plug-in he makes it in a separate dll, and MSDN states that an assembly with a strong name can only load other assemblies that are signed with the same private key...
Or have I got something wrong...
- Anders
Money talks, but all mine ever says is "Goodbye!"
ShotKeeper, my Photo Album / Organizer Application[^]My Photos[^]
|
|
|
|
|