Click here to Skip to main content
15,896,201 members
Home / Discussions / C#
   

C#

 
AnswerRe: Draw gradient image Pin
Amar Chaudhary12-Sep-07 0:33
Amar Chaudhary12-Sep-07 0:33 
QuestionProblem: tabControl (TabAlignment.Left) Pin
Soo Star11-Sep-07 23:39
Soo Star11-Sep-07 23:39 
AnswerRe: Problem: tabControl (TabAlignment.Left) Pin
Martin#12-Sep-07 8:35
Martin#12-Sep-07 8:35 
Questionequivalent of DocumentCompleted in WebBrowser while loading with AJAX Pin
scoroop11-Sep-07 23:37
scoroop11-Sep-07 23:37 
AnswerRe: equivalent of DocumentCompleted in WebBrowser while loading with AJAX Pin
scoroop12-Sep-07 3:15
scoroop12-Sep-07 3:15 
QuestionC# [DllImport()] questions. Pin
ioctl5111-Sep-07 23:34
ioctl5111-Sep-07 23:34 
AnswerRe: C# [DllImport()] questions. Pin
Malcolm Smart12-Sep-07 0:27
Malcolm Smart12-Sep-07 0:27 
AnswerRe: C# [DllImport()] questions. Pin
Luc Pattyn12-Sep-07 2:51
sitebuilderLuc Pattyn12-Sep-07 2:51 
Hi,

Q1.
parameter type does not have to be exact, so you can e.g. mix int, uint, Int32 (anything
that is a 32-bit integer). Be careful with long (64-bit in C#, 32-bit in native C), and
use some int type when native is bool.

Q2.
pointers to basic types (such as int*) just need a ref keyword
read-only string is just string
writeable string buffer is StringBuilder with sufficient initial capacity
pointers to a composite type are more complex; either:
- specify the correct type and hope the default marshalling will work correctly for you;
- specify the correct type and add marshalling attributes as appropriate (complex stuff!);
- or specify an IntPtr, but then all you get is the pointer value, so you still must solve
the marshalling; typically you would use Marshal.PtrToStructure, PtrToStringAnsi, etc.
Warning: IntPtr is 4B or 8B depending on OS; you must use IntPtr for all Windows handles.

Q3.
I never use unsafe or void*. I prefer to use same things as in Q2.

Q4.
Use out or ref keyword for value types (int, struct) that need a pointer so the native
code can change the value.


In general, if you are in charge of both sides, keep the interface simple; I prefer to:
- work with ints and strings, occasionally a struct containing ints;
- avoid structs/classes containing other classes (including strings);
- allocate memory in managed code, not have native code create stuff and return a pointer.

Warning: if you must pass a pointer from managed to unmanaged and the marshalling does not
make clear it is a pointer (it would know with string, StringBuilder, etc. but not with
IntPtr), then you must use the GCHandle class and pin the object.

Final comment: I tend to experiment with P/Invoke and put a lot of logging statements
at both managed and unmanaged sides (the first function I implement is logging a string
from unmanaged to managed world, so I can collect and display (Console.WriteLine, ListBox...)
logging from both sides on the managed side. That way you can easily see where things start
to go wrong.(This takes a delegate as a function pointer, you did not ask that yet!).

Smile | :)

Luc Pattyn [Forum Guidelines] [My Articles]


this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google


Questiondata grid view Vs databinding Pin
iet200011-Sep-07 22:52
iet200011-Sep-07 22:52 
AnswerRe: data grid view Vs databinding Pin
Waleed Eissa11-Sep-07 23:12
Waleed Eissa11-Sep-07 23:12 
GeneralRe: data grid view Vs databinding Pin
iet200011-Sep-07 23:43
iet200011-Sep-07 23:43 
GeneralRe: data grid view Vs databinding Pin
Waleed Eissa11-Sep-07 23:58
Waleed Eissa11-Sep-07 23:58 
AnswerRe: data grid view Vs databinding Pin
Nouman Bhatti12-Sep-07 0:06
Nouman Bhatti12-Sep-07 0:06 
Questionnew namespaces?? Pin
Tarun.Suneja11-Sep-07 22:21
Tarun.Suneja11-Sep-07 22:21 
AnswerRe: new namespaces?? Pin
Waleed Eissa11-Sep-07 23:13
Waleed Eissa11-Sep-07 23:13 
AnswerRe: new namespaces?? Pin
blackjack215012-Sep-07 0:29
blackjack215012-Sep-07 0:29 
AnswerRe: new namespaces?? Pin
Malcolm Smart12-Sep-07 0:29
Malcolm Smart12-Sep-07 0:29 
QuestionRe: new namespaces?? Pin
User 665812-Sep-07 0:33
User 665812-Sep-07 0:33 
GeneralRe: storing data from textbox into different files Pin
Ermak8611-Sep-07 22:12
Ermak8611-Sep-07 22:12 
Questionno overrideable in c# Pin
Adnan Siddiqi11-Sep-07 22:00
Adnan Siddiqi11-Sep-07 22:00 
AnswerRe: no overrideable in c# Pin
Pete O'Hanlon11-Sep-07 22:14
mvePete O'Hanlon11-Sep-07 22:14 
GeneralRe: no overrideable in c# Pin
Adnan Siddiqi11-Sep-07 22:50
Adnan Siddiqi11-Sep-07 22:50 
GeneralRe: no overrideable in c# Pin
Pete O'Hanlon12-Sep-07 1:32
mvePete O'Hanlon12-Sep-07 1:32 
AnswerRe: no overrideable in c# Pin
Vikram A Punathambekar11-Sep-07 23:59
Vikram A Punathambekar11-Sep-07 23:59 
QuestionZip files Pin
minniemooo11-Sep-07 21:45
minniemooo11-Sep-07 21:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.