Click here to Skip to main content
15,884,629 members
Home / Discussions / C#
   

C#

 
GeneralRe: load images dynamically in to sysetem resourses Pin
Zap-Man21-Oct-09 4:42
Zap-Man21-Oct-09 4:42 
GeneralRe: load images dynamically in to sysetem resourses Pin
Not Active21-Oct-09 4:52
mentorNot Active21-Oct-09 4:52 
GeneralRe: load images dynamically in to sysetem resourses Pin
Zap-Man21-Oct-09 5:05
Zap-Man21-Oct-09 5:05 
GeneralRe: load images dynamically in to sysetem resourses Pin
Not Active21-Oct-09 5:19
mentorNot Active21-Oct-09 5:19 
GeneralRe: load images dynamically in to sysetem resourses Pin
Zap-Man22-Oct-09 7:29
Zap-Man22-Oct-09 7:29 
Questionushort* to IntPtr conversion Pin
Jason McPeak20-Oct-09 12:38
Jason McPeak20-Oct-09 12:38 
AnswerRe: ushort* to IntPtr conversion [modified] Pin
Richard Andrew x6420-Oct-09 12:58
professionalRichard Andrew x6420-Oct-09 12:58 
AnswerRe: ushort* to IntPtr conversion Pin
Luc Pattyn20-Oct-09 13:56
sitebuilderLuc Pattyn20-Oct-09 13:56 
Hi,

I know two ways to do this without explicit marshaling, and in such a way that the data never gets copied. The example holds a native function that sums the elements of a managed array; however it would be equally valid for the native code to write into the array (within its bounds of course).

1.
for a once-of pointer, that is not used asynchronously in the native world later on, this suffices (it does need the "allow unsafe code" switch):
unsafe public int ArrayFixed() {
    int dim=1000;
    int[] numbers=new int[dim];
    ...
    int sum;
    fixed (int* pNumbers=&numbers[0]) {
        sum=SumArray(pNumbers, dim);
    }
    return sum;
}

[DllImport("native.dll")]
unsafe public static extern int SumArray(int* pNumbers, int count);


2.
The general approach (which I prefer) is this:
public int ArrayHandle() {
    int dim=10000;
    int[] numbers=new int[dim];
    ...
    GCHandle handle=GCHandle.Alloc(numbers, GCHandleType.Pinned);
    int sum=SumArray(handle.AddrOfPinnedObject(), dim);
    handle.Free();
    return sum;
}

[DllImport("nativeC.dll")]
public static extern int SumArray(IntPtr pNumbers, int count);

Here you should free the handle when the pointer is no longer in use natively.

Smile | :)

Luc Pattyn

I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages

Local announcement (Antwerp region): Lange Wapper? 59.24% waren verstandig genoeg om NEEN te stemmen; bye bye viaduct.


modified on Tuesday, October 20, 2009 8:05 PM

GeneralRe: ushort* to IntPtr conversion Pin
Jason McPeak20-Oct-09 14:34
Jason McPeak20-Oct-09 14:34 
GeneralRe: ushort* to IntPtr conversion Pin
Jason McPeak21-Oct-09 3:29
Jason McPeak21-Oct-09 3:29 
GeneralRe: ushort* to IntPtr conversion Pin
Luc Pattyn21-Oct-09 3:51
sitebuilderLuc Pattyn21-Oct-09 3:51 
QuestionPassing List to form and back... Help plz. Pin
JollyMansArt20-Oct-09 12:24
JollyMansArt20-Oct-09 12:24 
QuestionRe: Passing List to form and back... Help plz. Pin
JollyMansArt20-Oct-09 12:29
JollyMansArt20-Oct-09 12:29 
AnswerRe: Passing List to form and back... Help plz. Pin
Christian Graus20-Oct-09 13:01
protectorChristian Graus20-Oct-09 13:01 
QuestionRe: Passing List to form and back... Help plz. [modified] Pin
JollyMansArt20-Oct-09 13:07
JollyMansArt20-Oct-09 13:07 
AnswerRe: Passing List to form and back... Help plz. Pin
Christian Graus20-Oct-09 13:24
protectorChristian Graus20-Oct-09 13:24 
GeneralRe: Passing List to form and back... Help plz. Pin
JollyMansArt20-Oct-09 13:38
JollyMansArt20-Oct-09 13:38 
AnswerRe: Passing List to form and back... Help plz. Pin
Luc Pattyn20-Oct-09 14:03
sitebuilderLuc Pattyn20-Oct-09 14:03 
GeneralRe: Passing List to form and back... Help plz. Pin
JollyMansArt20-Oct-09 14:14
JollyMansArt20-Oct-09 14:14 
QuestionRe: Passing List to form and back... Help plz. [modified] Pin
JollyMansArt20-Oct-09 14:19
JollyMansArt20-Oct-09 14:19 
QuestionRe: Passing List to form and back... Help plz. Pin
JollyMansArt20-Oct-09 14:23
JollyMansArt20-Oct-09 14:23 
QuestionRe: Passing List to form and back... Help plz. Pin
JollyMansArt20-Oct-09 14:20
JollyMansArt20-Oct-09 14:20 
AnswerRe: Passing List to form and back... Help plz. Pin
JollyMansArt20-Oct-09 14:42
JollyMansArt20-Oct-09 14:42 
GeneralRe: Passing List to form and back... Help plz. Pin
Luc Pattyn20-Oct-09 14:32
sitebuilderLuc Pattyn20-Oct-09 14:32 
GeneralRe: Passing List to form and back... Help plz. Pin
JollyMansArt20-Oct-09 14:46
JollyMansArt20-Oct-09 14:46 

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.