I came across this issue recently. I was trying to access CreateProcess using DLLImport from managed code using C# and VB.Net. Here is my code from C#:

 

using System;

using System.Diagnostics;

using System.Runtime.InteropServices;

 

public struct PROCESS_INFORMATION

{

    public IntPtr hProcess;

    public IntPtr hThread;

    public uint dwProcessId;

    public uint dwThreadId;

}

 

public struct STARTUPINFO

{

    public uint cb;

    public string lpReserved;

    public string lpDesktop;

    public string lpTitle;

    public uint dwX;

    public uint dwY;

    public uint dwXSize;

    public uint dwYSize;

    public uint dwXCountChars;

    public uint dwYCountChars;

    public uint dwFillAttribute;

    public uint dwFlags;

    public short wShowWindow;

    public short cbReserved2;

    public IntPtr lpReserved2;

    public IntPtr hStdInput;

    public IntPtr hStdOutput;

    public IntPtr hStdError;

}

 

public struct SECURITY_ATTRIBUTES

{

    public int length;

    public IntPtr lpSecurityDescriptor;

    public bool bInheritHandle;

}

 

public class Program

{

    public static void Main()

    {

        STARTUPINFO si = new STARTUPINFO();

        PROCESS_INFORMATION pi = new PROCESS_INFORMATION();

        CreateProcess("C:\\WINDOWS\\SYSTEM32\\Calc.exe", null, IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, null, ref si, out pi);

        Console.ReadLine();

    }

 

[DllImport("kernel32.dll")]

static extern bool CreateProcess(string lpApplicationName, string lpCommandLine, IntPtr lpProcessAttributes, IntPtr lpThreadAttributes,

                        bool bInheritHandles, uint dwCreationFlags, IntPtr lpEnvironment,

                        string lpCurrentDirectory, ref STARTUPINFO lpStartupInfo,out PROCESS_INFORMATION lpProcessInformation);

}

 

 

This works like a charm. I initially had problems with this VB code. I looked at the IL code for both and figured out that I had constructed structures in C# for Process_Information, Security_Attricutes etc, but had marked them as classes in VB. This was mking the call to fail. I figured this out after looking into the IL from VB and C# to see what was different between the two.  I changed the VB to struct and it works like a charm in VB too. Check the VB code below!

 

 

Imports System.Runtime.InteropServices

Imports System.Security.Permissions

Imports System.Reflection

 

Module Module1

    Sub Main()

        StartupNotepad()

        Console.ReadLine()

    End Sub

    Sub StartupNotepad()

        Dim retValue As Boolean

        Dim pInfo As PROCESS_INFORMATION = New PROCESS_INFORMATION()

        Dim sInfo As STARTUPINFO = New STARTUPINFO()

 

        retValue = CreateProcess("c:\\windows\\system32\\NotePad.exe", Nothing, IntPtr.Zero, IntPtr.Zero, False, 0, IntPtr.Zero, Nothing, sInfo, pInfo)

 

    End Sub

    <StructLayout(LayoutKind.Sequential)> _

    Public Structure PROCESS_INFORMATION

        Public hProcess As IntPtr

        Public hThread As IntPtr

        Public dwProcessID As UInteger

        Public dwThreadID As UInteger

    End Structure 'PROCESS_INFORMATION

 

    <StructLayout(LayoutKind.Sequential)> _

    Public Structure SECURITY_ATTRIBUTES

        Public nLength As Integer

        Public lpSecurityDescriptor As IntPtr

        Public bInheritHandle As Boolean

    End Structure 'SECURITY_ATTRIBUTES

 

    <StructLayout(LayoutKind.Sequential)> _

    Public Structure STARTUPINFO

        Public cb As UInteger

        Public lpReserved As String

        Public lpDesktop As String

        Public lpTitle As String

        Public dwX As UInteger

        Public dwY As UInteger

        Public dwXSize As UInteger

        Public dwYSize As UInteger

        Public dwXCountChars As UInteger

        Public dwYCountChars As UInteger

        Public dwFillAttribute As UInteger

        Public dwFlags As UInteger

        Public wShowWindow As Short

        Public cbReserved2 As Short

        Public lpReserved2 As IntPtr

        Public hStdInput As IntPtr

        Public hStdOutput As IntPtr

        Public hStdError As IntPtr

    End Structure 'STARTINFO

    <DllImport("kernel32.dll")> _

    Function CreateProcess( _

    ByVal lpApplicationName As String, _

    ByVal lpCommandLine As String, _

    ByVal lpProcessAttributes As IntPtr, _

    ByVal lpThreadAttributes As IntPtr, _

    ByVal bInheritHandles As Boolean, _

    ByVal dwCreationFlags As UInteger, _

    ByVal lpEnvironment As IntPtr, _

    ByVal lpCurrentDirectory As String, _

    ByRef lpStartupInfo As STARTUPINFO, _

    ByRef lpProcessInformation As PROCESS_INFORMATION) As Boolean

    End Function

End Module