Click here to Skip to main content
15,902,835 members
Home / Discussions / C#
   

C#

 
GeneralRe: multiple selected checkboxes Pin
Yustme17-Oct-06 11:06
Yustme17-Oct-06 11:06 
QuestionProgrammatically resize ListView columns to view all text Pin
kozu17-Oct-06 9:40
kozu17-Oct-06 9:40 
AnswerRe: Programmatically resize ListView columns to view all text Pin
mav.northwind17-Oct-06 9:44
mav.northwind17-Oct-06 9:44 
GeneralRe: Programmatically resize ListView columns to view all text Pin
kozu17-Oct-06 13:09
kozu17-Oct-06 13:09 
Questionrefresh Pin
m.m._200717-Oct-06 9:28
m.m._200717-Oct-06 9:28 
AnswerRe: refresh Pin
Expert Coming17-Oct-06 12:28
Expert Coming17-Oct-06 12:28 
QuestionDataview.Rowfilter range Pin
mohit.raghav17-Oct-06 9:12
mohit.raghav17-Oct-06 9:12 
QuestionCalling unmanaged dll function from C# Pin
cpshadle17-Oct-06 8:07
cpshadle17-Oct-06 8:07 
I've been given the task of upgrading an old VB6 application to C#, and I think I'm starting to go down for the third time.... (and yes I've been reading miriads of posts, but not getting it).

The old application used a dll which still needs to be called in the new c# application.

In the vb6 code, the dll function needed was declared like this:
Declare Sub neededFunciton Lib "UnmanagedDLL.dll" (ByRef nestedStruc As NESTED_STRUC_TYPE)

where....
Public Type NESTED_STRUC_TYPE
    iNum1 As Long  
    fNum2(coniMaxSize) As Single
    nextNestedStruc As NEXT_NESTED_STRUC_TYPE   .
End Type

Public Type NEXT_NESTED_STRUC_TYPE
    iAnotherNum1 As Long
    fAnotherNum2(coniMaxSize) As Single    
    THIRDNestedStruc As THIRD_NESTED_STRUC_TYPE
End Type

Public Type THIRD_NESTED_STRUC_TYPE
    fArray1(coniMaxSize) As Single   
    fArray2(coniMaxSize) As Single 
    fArray3(coniMaxSize) As Single
    fArray4(coniMaxSize) As Single 
End Type

Public Const coniMaxSize As Integer = 144

In C#, I created a class BigObject containing all that struct stuff and the declarations:

[DllImport(@"C:\WINDOWS\UnmanagedDLL.dll", EntryPoint = "NeededFunciton")]
internal static extern int neededFunciton(ref NESTED_STRUC_TYPE nestedStruc);

After reading some examples I included this:

[DllImport("kernel32.dll", SetLastError=true)]
public static extern IntPtr LoadLibrary(string dllToLoad);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool FreeLibrary(IntPtr hModule);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate BigObject MyDelegate(ref BigObject.NESTED_STRUC_TYPE nestedStruc);


internal int NeededFunciton(ref NESTED_STRUC_TYPE nestedStruc)
{
    // Call LoadLibrary and GetProcAddress to get the address of the function
    // within the UnmanagedDLL.dll

    // LoadLibrary - returns a handle to the UnmanagedDLL.dll
    IntPtr pDll = LoadLibrary(@"C:\WINDOWS\UnmanagedDLL.dll");
    if (pDll != IntPtr.Zero)
    {
    // GetProcAddress - obtain the address of an exported function within
    // the previously loaded dll
    IntPtr pAddressOfFunctionToCall = GetProcAddress(pDll, "NeededFunciton");
    if (pAddressOfFunctionToCall != IntPtr.Zero)
    {
        // Now use the GetDelegateForFunctionPointer static method within the Marshal class
        // to assign this address to a predefined C# delegate
        MyDelegate myDelegate = (MyDelegate)Marshal.GetDelegateForFunctionPointer(
        pAddressOfFunctionToCall,
        typeof(MyDelegate));
        try
        {
        myDelegate(ref nestedStruc);
        }
        catch (Exception e)
        {
       
        //todo, changed exception
        }
    }
    }
    return Convert.ToInt16 (FreeLibrary(pDll));
}

Finally, in another class, I try to call the NeededFunciton with the nestedStruc which has been prepopulated with data:
bigObject.NeededFunciton(ref nestedStruc);

I'm getting
FatalExecutionEngineError was detected
Message: The runtime has encountered a fatal error.
The address of the error was at 0x79f1c184, on thread 0x970. The error code is 0xc0000005.
This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code.
Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.


OK, with all those nested strucs, I'm guessing I'm getting marshaling errors.
Can someone shoot me to a good site ref. or add a bit of insight that might kick start me again?
I'm afraid I have chronic brain cramp....
AnswerRe: Calling unmanaged dll function from C# Pin
Ed.Poore17-Oct-06 10:06
Ed.Poore17-Oct-06 10:06 
AnswerRe: Calling unmanaged dll function from C# Pin
Nader Elshehabi17-Oct-06 21:02
Nader Elshehabi17-Oct-06 21:02 
QuestionGeneric Dictionary - Using custom types as the Key Pin
Dominic Pettifer17-Oct-06 7:33
Dominic Pettifer17-Oct-06 7:33 
AnswerRe: Generic Dictionary - Using custom types as the Key Pin
Ed.Poore17-Oct-06 8:09
Ed.Poore17-Oct-06 8:09 
QuestionHow to Create icon Object from Stream ? Pin
hdv21217-Oct-06 6:12
hdv21217-Oct-06 6:12 
AnswerRe: How to Create icon Object from Stream ? Pin
Judah Gabriel Himango17-Oct-06 6:58
sponsorJudah Gabriel Himango17-Oct-06 6:58 
GeneralRe: How to Create icon Object from Stream ? Pin
hdv21217-Oct-06 7:56
hdv21217-Oct-06 7:56 
GeneralRe: How to Create icon Object from Stream ? Pin
Judah Gabriel Himango17-Oct-06 8:25
sponsorJudah Gabriel Himango17-Oct-06 8:25 
AnswerRe: How to Create icon Object from Stream ? Pin
Eric Dahlvang17-Oct-06 8:40
Eric Dahlvang17-Oct-06 8:40 
GeneralRe: How to Create icon Object from Stream ? Pin
hdv21217-Oct-06 11:40
hdv21217-Oct-06 11:40 
GeneralRe: How to Create icon Object from Stream ? Pin
Eric Dahlvang18-Oct-06 3:27
Eric Dahlvang18-Oct-06 3:27 
AnswerRe: How to Create icon Object from Stream ? Pin
panks_r17-Oct-06 18:59
panks_r17-Oct-06 18:59 
QuestionRe: How to Create icon Object from Stream ? Pin
hfrmobile27-Nov-06 7:04
hfrmobile27-Nov-06 7:04 
QuestionNumerical Sort of DataTable Pin
bward8417-Oct-06 5:51
bward8417-Oct-06 5:51 
AnswerRe: Numerical Sort of DataTable Pin
Guffa17-Oct-06 7:06
Guffa17-Oct-06 7:06 
Questionimage FileNotFound exception on shared server? Pin
Goalie3517-Oct-06 5:40
Goalie3517-Oct-06 5:40 
AnswerRe: image FileNotFound exception on shared server? Pin
Judah Gabriel Himango17-Oct-06 7:30
sponsorJudah Gabriel Himango17-Oct-06 7:30 

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.