Question How Attach Resource To Compiled EXE And Receive That?

yashar

New member
Joined
Oct 28, 2015
Messages
1
Programming Experience
3-5
Hi All.

I use this code to write resource to exe file :

VB.NET:
[COLOR=#00008B]Module[/COLOR][COLOR=#000000] ResourceWriter
    [/COLOR][COLOR=#00008B]Private[/COLOR][COLOR=#00008B]Function[/COLOR][COLOR=#000000] ToPtr[/COLOR][COLOR=#000000]([/COLOR][COLOR=#00008B]ByVal[/COLOR][COLOR=#000000] data [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#00008B]Object[/COLOR][COLOR=#000000])[/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] IntPtr
        [/COLOR][COLOR=#00008B]Dim[/COLOR][COLOR=#000000] h [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] GCHandle [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] GCHandle[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Alloc[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]data[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] GCHandleType[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Pinned[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#00008B]Dim[/COLOR][COLOR=#000000] ptr [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] IntPtr
        [/COLOR][COLOR=#00008B]Try[/COLOR][COLOR=#000000]
            ptr [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] h[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]AddrOfPinnedObject[/COLOR][COLOR=#000000]()[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#00008B]Finally[/COLOR][COLOR=#000000]
            h[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Free[/COLOR][COLOR=#000000]()[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#00008B]End[/COLOR][COLOR=#00008B]Try[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#00008B]Return[/COLOR][COLOR=#000000] ptr


    [/COLOR][COLOR=#00008B]End[/COLOR][COLOR=#00008B]Function[/COLOR][COLOR=#000000]


    [/COLOR][COLOR=#000000]<[/COLOR][COLOR=#000000]DllImport[/COLOR][COLOR=#000000]([/COLOR][COLOR=#800000]"kernel32.dll"[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] SetLastError[/COLOR][COLOR=#000000]:=[/COLOR][COLOR=#800000]True[/COLOR][COLOR=#000000])>[/COLOR][COLOR=#000000]
    [/COLOR][COLOR=#00008B]Private[/COLOR][COLOR=#00008B]Function[/COLOR][COLOR=#000000] UpdateResource[/COLOR][COLOR=#000000]([/COLOR][COLOR=#00008B]ByVal[/COLOR][COLOR=#000000] hUpdate [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] IntPtr[/COLOR][COLOR=#000000],[/COLOR][COLOR=#00008B]ByVal[/COLOR][COLOR=#000000] lpType [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#00008B]String[/COLOR][COLOR=#000000],[/COLOR][COLOR=#00008B]ByVal[/COLOR][COLOR=#000000] lpName [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#00008B]String[/COLOR][COLOR=#000000],[/COLOR][COLOR=#00008B]ByVal[/COLOR][COLOR=#000000] wLanguage [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] UShort[/COLOR][COLOR=#000000],[/COLOR][COLOR=#00008B]ByVal[/COLOR][COLOR=#000000] lpData [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] IntPtr[/COLOR][COLOR=#000000],[/COLOR][COLOR=#00008B]ByVal[/COLOR][COLOR=#000000] cbData [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] UInteger[/COLOR][COLOR=#000000])[/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#00008B]Boolean[/COLOR][COLOR=#000000]
    [/COLOR][COLOR=#00008B]End[/COLOR][COLOR=#00008B]Function[/COLOR][COLOR=#000000]
    [/COLOR][COLOR=#000000]<[/COLOR][COLOR=#000000]DllImport[/COLOR][COLOR=#000000]([/COLOR][COLOR=#800000]"kernel32.dll"[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] SetLastError[/COLOR][COLOR=#000000]:=[/COLOR][COLOR=#800000]True[/COLOR][COLOR=#000000])>[/COLOR][COLOR=#000000]
    [/COLOR][COLOR=#00008B]Private[/COLOR][COLOR=#00008B]Function[/COLOR][COLOR=#000000] BeginUpdateResource[/COLOR][COLOR=#000000]([/COLOR][COLOR=#00008B]ByVal[/COLOR][COLOR=#000000] pFileName [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#00008B]String[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000]<[/COLOR][COLOR=#000000]MarshalAs[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]UnmanagedType[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Bool[/COLOR][COLOR=#000000])>[/COLOR][COLOR=#00008B]ByVal[/COLOR][COLOR=#000000] bDeleteExistingResources [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#00008B]Boolean[/COLOR][COLOR=#000000])[/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] IntPtr
    [/COLOR][COLOR=#00008B]End[/COLOR][COLOR=#00008B]Function[/COLOR][COLOR=#000000]
    [/COLOR][COLOR=#000000]<[/COLOR][COLOR=#000000]DllImport[/COLOR][COLOR=#000000]([/COLOR][COLOR=#800000]"kernel32.dll"[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] SetLastError[/COLOR][COLOR=#000000]:=[/COLOR][COLOR=#800000]True[/COLOR][COLOR=#000000])>[/COLOR][COLOR=#000000]
    [/COLOR][COLOR=#00008B]Private[/COLOR][COLOR=#00008B]Function[/COLOR][COLOR=#000000] EndUpdateResource[/COLOR][COLOR=#000000]([/COLOR][COLOR=#00008B]ByVal[/COLOR][COLOR=#000000] hUpdate [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] IntPtr[/COLOR][COLOR=#000000],[/COLOR][COLOR=#00008B]ByVal[/COLOR][COLOR=#000000] fDiscard [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#00008B]Boolean[/COLOR][COLOR=#000000])[/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#00008B]Boolean[/COLOR][COLOR=#000000]
    [/COLOR][COLOR=#00008B]End[/COLOR][COLOR=#00008B]Function[/COLOR][COLOR=#000000]




    [/COLOR][COLOR=#00008B]Public[/COLOR][COLOR=#00008B]Function[/COLOR][COLOR=#000000] WriteResource[/COLOR][COLOR=#000000]([/COLOR][COLOR=#00008B]ByVal[/COLOR][COLOR=#000000] filename [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#00008B]String[/COLOR][COLOR=#000000],[/COLOR][COLOR=#00008B]ByVal[/COLOR][COLOR=#000000] bytes [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#00008B]Byte[/COLOR][COLOR=#000000]())[/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#00008B]Boolean[/COLOR][COLOR=#000000]


        [/COLOR][COLOR=#00008B]Try[/COLOR][COLOR=#000000]
            [/COLOR][COLOR=#00008B]Dim[/COLOR][COLOR=#000000] handle [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] IntPtr [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] BeginUpdateResource[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]filename[/COLOR][COLOR=#000000],[/COLOR][COLOR=#800000]False[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]
            [/COLOR][COLOR=#00008B]Dim[/COLOR][COLOR=#000000] file1 [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#00008B]Byte[/COLOR][COLOR=#000000]()[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] bytes
            [/COLOR][COLOR=#00008B]Dim[/COLOR][COLOR=#000000] fileptr [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] IntPtr [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] ToPtr[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]file1[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]
            [/COLOR][COLOR=#00008B]Dim[/COLOR][COLOR=#000000] res [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#00008B]Boolean[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] UpdateResource[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]handle[/COLOR][COLOR=#000000],[/COLOR][COLOR=#800000]"RT_RCDATA"[/COLOR][COLOR=#000000],[/COLOR][COLOR=#800000]"0"[/COLOR][COLOR=#000000],[/COLOR][COLOR=#800000]0[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] fileptr[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] Convert[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]ToUInt32[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]file1[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Length[/COLOR][COLOR=#000000]))[/COLOR][COLOR=#000000]
            EndUpdateResource[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]handle[/COLOR][COLOR=#000000],[/COLOR][COLOR=#800000]False[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#00008B]Catch[/COLOR][COLOR=#000000] ex [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] Exception
            [/COLOR][COLOR=#00008B]Return[/COLOR][COLOR=#800000]False[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#00008B]End[/COLOR][COLOR=#00008B]Try[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#00008B]Return[/COLOR][COLOR=#800000]True[/COLOR][COLOR=#000000]


    [/COLOR][COLOR=#00008B]End[/COLOR][COLOR=#00008B]Function[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#00008B]End[/COLOR][COLOR=#00008B]Module[/COLOR]


And This Code For Receive Resource In Second EXE (I have The 2nd exe source same in vb.net)

VB.NET:
[COLOR=#00008B]Module[/COLOR][COLOR=#000000] gsoijdMkeytY
    [/COLOR][COLOR=#000000]<[/COLOR][COLOR=#000000]DllImport[/COLOR][COLOR=#000000]([/COLOR][COLOR=#800000]"kernel32.dll"[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] SetLastError[/COLOR][COLOR=#000000]:=[/COLOR][COLOR=#800000]True[/COLOR][COLOR=#000000])>[/COLOR][COLOR=#000000]
    [/COLOR][COLOR=#00008B]Private[/COLOR][COLOR=#00008B]Function[/COLOR][COLOR=#000000] FindResource[/COLOR][COLOR=#000000]([/COLOR][COLOR=#00008B]ByVal[/COLOR][COLOR=#000000] ZlsTnllYezlL [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] IntPtr[/COLOR][COLOR=#000000],[/COLOR][COLOR=#00008B]ByVal[/COLOR][COLOR=#000000] lpName [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#00008B]String[/COLOR][COLOR=#000000],[/COLOR][COLOR=#00008B]ByVal[/COLOR][COLOR=#000000] lpType [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#00008B]String[/COLOR][COLOR=#000000])[/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] IntPtr
    [/COLOR][COLOR=#00008B]End[/COLOR][COLOR=#00008B]Function[/COLOR][COLOR=#000000]
    [/COLOR][COLOR=#00008B]Private[/COLOR][COLOR=#00008B]Declare[/COLOR][COLOR=#00008B]Function[/COLOR][COLOR=#000000] JNBJGAPChMdi [/COLOR][COLOR=#00008B]Lib[/COLOR][COLOR=#800000]"kernel32"[/COLOR][COLOR=#00008B]Alias[/COLOR][COLOR=#800000]"GetModuleHandleA"[/COLOR][COLOR=#000000]([/COLOR][COLOR=#00008B]ByVal[/COLOR][COLOR=#000000] moduleName [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#00008B]String[/COLOR][COLOR=#000000])[/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] IntPtr
    [/COLOR][COLOR=#00008B]Private[/COLOR][COLOR=#00008B]Declare[/COLOR][COLOR=#00008B]Function[/COLOR][COLOR=#000000] SizeofResource [/COLOR][COLOR=#00008B]Lib[/COLOR][COLOR=#800000]"kernel32"[/COLOR][COLOR=#000000]([/COLOR][COLOR=#00008B]ByVal[/COLOR][COLOR=#000000] ZlsTnllYezlL [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] IntPtr[/COLOR][COLOR=#000000],[/COLOR][COLOR=#00008B]ByVal[/COLOR][COLOR=#000000] hResInfo [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] IntPtr[/COLOR][COLOR=#000000])[/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#00008B]Integer[/COLOR][COLOR=#000000]
    [/COLOR][COLOR=#00008B]Private[/COLOR][COLOR=#00008B]Declare[/COLOR][COLOR=#00008B]Function[/COLOR][COLOR=#000000] LoadResource [/COLOR][COLOR=#00008B]Lib[/COLOR][COLOR=#800000]"kernel32"[/COLOR][COLOR=#000000]([/COLOR][COLOR=#00008B]ByVal[/COLOR][COLOR=#000000] ZlsTnllYezlL [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] IntPtr[/COLOR][COLOR=#000000],[/COLOR][COLOR=#00008B]ByVal[/COLOR][COLOR=#000000] hResInfo [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] IntPtr[/COLOR][COLOR=#000000])[/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] IntPtr
    [/COLOR][COLOR=#00008B]Public[/COLOR][COLOR=#00008B]Function[/COLOR][COLOR=#000000] CoqalHyeEZps[/COLOR][COLOR=#000000]([/COLOR][COLOR=#00008B]ByVal[/COLOR][COLOR=#000000] MVHjXxNPwlcC [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#00008B]String[/COLOR][COLOR=#000000])[/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#00008B]Byte[/COLOR][COLOR=#000000]()[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#00008B]Dim[/COLOR][COLOR=#000000] ZlsTnllYezlL [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] IntPtr [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] JNBJGAPChMdi[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]MVHjXxNPwlcC[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#00008B]Dim[/COLOR][COLOR=#000000] PcOKouMcMJaW [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] IntPtr [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] FindResource[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]ZlsTnllYezlL[/COLOR][COLOR=#000000],[/COLOR][COLOR=#800000]"0"[/COLOR][COLOR=#000000],[/COLOR][COLOR=#800000]"RT_RCDATA"[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#00008B]Dim[/COLOR][COLOR=#000000] JDDcSAvEjWmg [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] IntPtr [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] LoadResource[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]ZlsTnllYezlL[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] PcOKouMcMJaW[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#00008B]Dim[/COLOR][COLOR=#000000] FOLUbFmExmUo [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] SizeofResource[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]ZlsTnllYezlL[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] PcOKouMcMJaW[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#00008B]Dim[/COLOR][COLOR=#000000] fEUUehyJxiz [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#00008B]Byte[/COLOR][COLOR=#000000]()[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#00008B]New[/COLOR][COLOR=#00008B]Byte[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]FOLUbFmExmUo [/COLOR][COLOR=#000000]-[/COLOR][COLOR=#800000]1[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]{}[/COLOR][COLOR=#000000]
        Marshal[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Copy[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]JDDcSAvEjWmg[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] fEUUehyJxiz[/COLOR][COLOR=#000000],[/COLOR][COLOR=#800000]0[/COLOR][COLOR=#000000],[/COLOR][COLOR=#00008B]CInt[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]FOLUbFmExmUo[/COLOR][COLOR=#000000]))[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#00008B]Return[/COLOR][COLOR=#000000] fEUUehyJxiz
    [/COLOR][COLOR=#00008B]End[/COLOR][COLOR=#00008B]Function[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#00008B]End[/COLOR][COLOR=#00008B]Module[/COLOR]


and when i want call the reader module use this one :

VB.NET:
[COLOR=#00008B]Dim[/COLOR][COLOR=#000000] getfile [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#00008B]Byte[/COLOR][COLOR=#000000]()[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] CoqalHyeEZps[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]Application[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]ExecutablePath[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]
    [/COLOR][COLOR=#00008B]Dim[/COLOR][COLOR=#000000] FileAsString [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] Encoding[/COLOR][COLOR=#000000].[/COLOR][COLOR=#00008B]Default[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]GetString[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]getfile[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]
    [/COLOR][COLOR=#00008B]Dim[/COLOR][COLOR=#000000] Info[/COLOR][COLOR=#000000]()[/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#00008B]String[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] Split[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]FileAsString[/COLOR][COLOR=#000000],[/COLOR][COLOR=#800000]"RT_RCDATA"[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]
    [/COLOR][COLOR=#00008B]Dim[/COLOR][COLOR=#000000] views [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#00008B]String[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] Info[/COLOR][COLOR=#000000]([/COLOR][COLOR=#800000]1[/COLOR][COLOR=#000000])[/COLOR]


But The problem is here! when run project in Line :

VB.NET:
[COLOR=#000000]Marshal[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Copy[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]JDDcSAvEjWmg[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] fEUUehyJxiz[/COLOR][COLOR=#000000],[/COLOR][COLOR=#800000]0[/COLOR][COLOR=#000000],[/COLOR][COLOR=#00008B]CInt[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]FOLUbFmExmUo[/COLOR][COLOR=#000000]))[/COLOR]


I get this error msg :

VB.NET:
[COLOR=#000000]An unhandled exception of type [/COLOR][COLOR=#808080]'System.ArgumentNullException' occurred in WindowsApplication1.exe[/COLOR]


Where is problem?
Is there any other way to attach resource and receive that on other exe?
In this one i dont know the resource attach with what name and what kind!
I want just some txt and do my condition by that for example attach 'Resource1="true"' and i will check Resource1 if is "true"!

Also tried using mono.cecil bu could not do any thing good!


thank you best regards.
 

Latest posts

Back
Top