|
|
Hi,
i have a dynamic link library (compiled in VC++ 7.0) and want to use the functions(and types,structs) in my c# program, i already read some articles on this msg-board about unmanaged code, just those only show easy functions with simple return-paramaters.
eg.:
[DllImport("kernel32")] public static extern void Sleep(int dwMilliseconds);
What if the functions return parameter is a pointer to a complex structure(eg.: chained lists)?
As example, i am trying to import functions from a packet construction library called libnet, alone the init function of this library looks like this:
libnet_t * libnet_init(int injection_type, int8_t *device, int8_t *err_buf);
My question now is can someone explain/show/link me how i marshall such return/call by reference variables, will i have to write a wrapper class which acts as a interface between my c# program and the c++ dll, if yes how?
If you can help provide an easy code snippet and show me how i need to start or point me to a book/website/etc.
Thanks in advance....
|
|
|
|
|
Declare the function with return type "IntPtr". An IntPtr can point to any address in memory, and you can de-marshal the object:
IntPtr ptr = libnet_init(...);
Object obj = Marshal.PtrToStructure(ptr);
to use the object in C#, you have to write an interface.
|
|
|
|
|
Hope you can give me a little bit more help anyway here is code snippet how i would use the functions / structs in c++ and create a simple TCP packet.
<br />
#include <libnet.h><br />
int main(int argc, char **argv)<br />
{<br />
libnet_t *lib_pointer = NULL;
libnet_ptag_t tcp,ip;
char *device = NULL;<br />
char errbuff[1024];<br />
write int;<br />
<br />
lib_pointer = libnet_init(LIBNET_RAW4, device, errbuff);<br />
<br />
tcp = libnet_build_tcp(..,..,..,.,lib_pointer,...);<br />
ip = libnet_build_ip(..,..,..,lib_pointer,.....);<br />
write=libnet_write(lib_pointer);<br />
My main question is how can i use the types, consts which are defined in libnet.h ala the .dll file i compiled in vc++. You made the statement about this IntPtr, which i marshal to a PtrToStruct, well so i could also create a PtrToStruct type in c# and let it point to eg. libnet_t?
What i did not really understand is the interface thing you mentioned, could you explain this a bit more deeply?
Thanks for your time again.
|
|
|
|
|
There are a few ways of doing this:
1. Hardest solution: Write a custom marshaller in Managed C++ that converts(or wraps) the non-managed type into a managed type;
2. Take the approach of returning an IntPtr, then defining a custom interface to the embedded type.
3. Easiest solution: If the return type can be decomposed into simpler managed variable types, define the structure in the managed part:
function decl:
my_type* getMyTypeStuff( .... );
where my_type =
struct my_type {
int foo;
int bar;
HWND* gobble;
char[] yadda;
}...;
managed declarations:
struct MyType {
Int32 foo;
Int32 bar;
IntPtr gobble;
byte[] yadda (or char[] yadda, or IntPtr yadda...)
}
[DllImport(....)]
public static extern MyType[] getMyTypeStuff(....);
You may need a MarshalAs attribute in there, not sure, I'm running from memory. Actually, take all of the above with a grain of salt, as I don't have any reference material in front of me at the moment...
Jeremy Kimball
|
|
|
|
|
You could also make use of unsafe coding by using the unsafe keyword. Make shure to set up the compiler options for allowing unsafe code in the project. For further instructions check the help provided with your c# package.
|
|
|
|
|
I like to define my class variables at the top of the file. When I add extra controls in the design view, the new item's definitions are slotted in between those I have already put there. I guess the auto-code-insertion is looking for the last private definition below the class keyword. Can anyone point me at information about how this is actually handled?
|
|
|
|
|
Hello!
I want to implement simple COM object which will communicate with Oracle database somewhere on internet. It looks rather simple. But I have a possibility to use either C++ or C# technology. As I do not know C# very well: someone can help and put some comments - which technology would be simpler to write/manage ?
Or maybe there exists some ready-to-use sample?
Thanks for help
|
|
|
|
|
If you are going to have to *learn* either C++ or C#, i'd suggest C#. However C# can very easily handle this with the .NET data provider classes. The link below is to an article on using the Oracle Data Provider for .NET.
Should give enough examples to help your decision out.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/manprooracperf.asp
|
|
|
|
|
The memory can't be release,after I used OleDBConnection.Close() to close a connection. But when I make the program minimal size, the memory released almost 90 percent. Can anyone help me to solve this problem? Thanks.
|
|
|
|
|
Memory is released when the garbage collector has time allotted to it. If you want to free memory immediately, make sure that all IDisposable implementations are disposed, or call GC.Collect which disposes objects and frees memory, although this isn't recommended.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
|
dear zhang,
be carfull that when you call connection .close() it will not release the memory but just sleep the connection to the server you can explore you server and find the session available after closeing the connection but to free the memry and release the session to the server you have to call dispose function which will do that
Mhmoud Rawas
------------
Software Eng.
|
|
|
|
|
thanks, but I changed to use dispose function to close the connection, the usage of memory displaying in the task manager is the same.
|
|
|
|
|
zhang_ding wrote:
the usage of memory displaying in the task manager is the same
BTW, that's not the most accurate measurement of application memory use. It doesn't necessarily reflect the actual usage - just the amount assigned to that process.
|
|
|
|
|
I have the standard edition of Visual Studio Net and I am trying to use the report generator from Crystal Reports. I can't find the report template when I try to add it as a new item. Is it not included with the standard edition or something?
If I want to make a report from a database, why would I not try to automate Access to do it? I've never done such a thing, but the book says you can do it. Is it something that is very cumbersome or something?
Thanks,
martinkilgore933#takethisout#@hotmail.com
|
|
|
|
|
When we use the SqlConnection,It has a Attribute named "Connectionstring".if we Click the Attribute and Select "new connection". A Form will popup.
The Question is : How can I use the "Poupup Form" in my Customer Control??
|
|
|
|
|
This is a designer interface. In order to use the designer interfaces, you have to host the designer. Even then, the designer in this case is Visual Studio .NET, so you won't be able to use the implementation. Some designer interfaces are in the .NET Framework itself (the BCL), but this one is not. The best you can do is develop something similar.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
This is not really what you want but you can still achieve a similar thing like this:
Add reference to
- Microsoft ActiveX Data Objects 2.7 Library
- Microsoft OLEDB Service Component 1.0 Type Library
In your code add this:
ADODB.Connection conn = new ADODB.ConnectionClass();<br />
MSDASC.DataLinks dLinks = new MSDASC.DataLinksClass();<br />
object cn = (object) conn;<br />
dLinks.PromptEdit(ref cn);<br />
string myConnStr = conn.ConnectionString;
The connection string returned cannot be used with SqlConnection. What you need to do is to parse the string and remove any occurence of Provider=SQLOLEDB.1 in order to pass this connection string to your SqlConnection. If you forget this, you will be blessed with the Exception.
Enjoy
|
|
|
|
|
Can anyone tell me how to fix a problem with the Windows Form Designer disapearing from a class after copying a class from one project to another?
I have had this happen a couple of times. I want to steal a forms class from an existing project to use in a new project. I copy both the .cs and rex files from one project directory to annother and then use the "Add Existing" option in Visual Studio to add the form code to the project fixing the namespace appropriately.
The project compiles fine and runs fine. The form works but does not show up as a form in the Solution Explorer. And, when you try to open the form in the forms designer, all you get is source coded, no visual interface.
Everything works, but you can't use the designer to work on the form.
I've looked at the source code for a new form and compared it to the old form and I don't see any differences. Can anyone shed any light on this issue? It's a pain...
Thanks...
|
|
|
|
|
that happens when u have more than one "root" class in a file.
leppie::AllocCPArticle("Zee blog"); Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.
|
|
|
|
|
VS.NET, for some reason, thinks that your code file does not contain a designable class. This can be for a number of reasons: most likely, you've got a class defined in that file before the Form-derived class.
To fix this, open up the .csproj file and look for the element
<File
RelPath = "YourFile.cs"
SubType = "Code"
BuildAction = "Compile"
/>
and change it to:
<File
RelPath = "YourFile.cs"
SubType = "Form"
BuildAction = "Compile"
/>
After that, open up this .csproj file in studio again, and it should show that file as a Form-derived class and allow you to open it with the Form Designer
--
Russell Morris
"So, broccoli, mother says you're good for me... but I'm afraid I'm no good for you!" - Stewy
|
|
|
|
|
hello, it's possible to load data from xml file, and then save it in database table, both data structures are the same, has xsd file definition?
see ya,
KrisPL
krzysztofo@poczta.onet.pl
|
|
|
|
|
|
i have an xml file generated by my program ex.:
<records>
<record id="1" name="test" date="2003-11-17" />
<record id="2" name="test2" date="2003-11-16" />
</records>
and Database table Table1 with structure:
id, int
name, varchar(50)
date, datetime
i want to store this data in database, i can do that by reading rows, step by step, but i search for some basic method, by using DataSet and DataAdapter (i think), i'm using DataSet.ReadXml method to read data from xml file but i don't know how to store it in db table, i were try methods DataAdapter like FillSchema ect. but i fill like blindman in this topic.
KrisPL
krzysztofo@poczta.onet.pl
|
|
|
|