Click here to Skip to main content
15,909,953 members
Home / Discussions / C#
   

C#

 
QuestionError while Extracting Files in zip folder Pin
arun_pk3-Jan-11 18:34
arun_pk3-Jan-11 18:34 
AnswerRe: Error while Extracting Files in zip folder Pin
OriginalGriff3-Jan-11 22:40
mveOriginalGriff3-Jan-11 22:40 
GeneralRe: Error while Extracting Files in zip folder Pin
arun_pk3-Jan-11 23:34
arun_pk3-Jan-11 23:34 
GeneralRe: Error while Extracting Files in zip folder Pin
OriginalGriff3-Jan-11 23:38
mveOriginalGriff3-Jan-11 23:38 
GeneralRe: Error while Extracting Files in zip folder Pin
arun_pk3-Jan-11 23:54
arun_pk3-Jan-11 23:54 
GeneralRe: Error while Extracting Files in zip folder Pin
OriginalGriff4-Jan-11 2:06
mveOriginalGriff4-Jan-11 2:06 
GeneralRe: Error while Extracting Files in zip folder Pin
arun_pk4-Jan-11 17:38
arun_pk4-Jan-11 17:38 
QuestionP/Invoke an array that is a field in a struct. Pin
DaveyM693-Jan-11 11:10
professionalDaveyM693-Jan-11 11:10 
This is managed to unmanaged only!

The C code (simplified) is:
C
typedef INT SomeTypeArray[128];
void SomeFunction(INT *lpSomeTypeArray);


So SomeFunction can just be passed an int[] as an array is a reference type - no problem.
However, I would prefer to have a struct SomeType and another struct SomeTypeArray.

This code does all this:
C#
public static class NativeMethods
{
    // void SomeFunction(INT *lpSomeTypeArray);
    [DllImport("some.dll", SetLastError = true)]
    public static extern void SomeFunction(
        SomeTypeArray lpSomeTypeArray);
}

[StructLayout(LayoutKind.Sequential)]
public struct SomeType
{
    private int value;

    private SomeType(int value)
    {
        this.value = value;
    }

    public static implicit operator SomeType(int value)
    {
        return new SomeType(value);
    }
    public static implicit operator int(SomeType someType)
    {
        return someType.value;
    }
}

// typedef INT SomeTypeArray[128];
[StructLayout(LayoutKind.Sequential)]
public struct SomeTypeArray
{
    private SomeType[] someTypes;

    public SomeTypeArray(params SomeType[] someTypes)
    {
        if (someTypes == null)
            this.someTypes = new SomeType[128];
        else if (someTypes.Length == 128)
            this.someTypes = someTypes;
        else
        {
            this.someTypes = new SomeType[128];
            int counter = 0;
            foreach (SomeType someType in someTypes)
            {
                this.someTypes[counter] = someType;
                counter++;
                if (counter == this.someTypes.Length)
                    break;
            }
        }
    }
}

The problem with this is that if SomeTypeArray's default constructor is called, someTypes will be null and therefore invalid - and being a struct the default constructor can't be overloaded and field initializers outside a constructor are not allowed!

Any workaround for this or a better way of doing it?
Dave

Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.
Astonish us. Be exceptional. (Pete O'Hanlon)

BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)



AnswerRe: P/Invoke an array that is a field in a struct. Pin
Luc Pattyn3-Jan-11 11:35
sitebuilderLuc Pattyn3-Jan-11 11:35 
GeneralRe: P/Invoke an array that is a field in a struct. Pin
DaveyM693-Jan-11 11:59
professionalDaveyM693-Jan-11 11:59 
GeneralRe: P/Invoke an array that is a field in a struct. Pin
Luc Pattyn3-Jan-11 12:06
sitebuilderLuc Pattyn3-Jan-11 12:06 
GeneralRe: P/Invoke an array that is a field in a struct. Pin
DaveyM693-Jan-11 12:11
professionalDaveyM693-Jan-11 12:11 
GeneralRe: P/Invoke an array that is a field in a struct. Pin
Luc Pattyn3-Jan-11 12:31
sitebuilderLuc Pattyn3-Jan-11 12:31 
GeneralRe: P/Invoke an array that is a field in a struct. Pin
DaveyM693-Jan-11 12:36
professionalDaveyM693-Jan-11 12:36 
GeneralRe: P/Invoke an array that is a field in a struct. Pin
DaveyM693-Jan-11 12:33
professionalDaveyM693-Jan-11 12:33 
AnswerRe: P/Invoke an array that is a field in a struct. Pin
_Erik_4-Jan-11 6:02
_Erik_4-Jan-11 6:02 
Questioncheck credentials on remote machine Pin
User 73794393-Jan-11 10:14
User 73794393-Jan-11 10:14 
QuestionPipe operator Pin
William Winner3-Jan-11 8:41
William Winner3-Jan-11 8:41 
AnswerRe: Pipe operator Pin
Ian Shlasko3-Jan-11 9:12
Ian Shlasko3-Jan-11 9:12 
GeneralRe: Pipe operator Pin
William Winner3-Jan-11 10:14
William Winner3-Jan-11 10:14 
GeneralRe: Pipe operator Pin
PIEBALDconsult3-Jan-11 11:45
mvePIEBALDconsult3-Jan-11 11:45 
GeneralRe: Pipe operator Pin
Ian Shlasko3-Jan-11 11:56
Ian Shlasko3-Jan-11 11:56 
GeneralRe: Pipe operator Pin
AspDotNetDev3-Jan-11 13:40
protectorAspDotNetDev3-Jan-11 13:40 
GeneralRe: Pipe operator Pin
PIEBALDconsult3-Jan-11 14:22
mvePIEBALDconsult3-Jan-11 14:22 
GeneralRe: Pipe operator Pin
Ian Shlasko3-Jan-11 15:58
Ian Shlasko3-Jan-11 15:58 

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.