Jump to content
Tuts 4 You

Use Dll From Resource


oricode

Recommended Posts

I am trying to bundle bassmod.net.dll along with a C# application. At present, I have included it in the resource and write it to disk upon execution.It works fine,but the problem is I can't delete the DLL upon app close. So I thought of using DLL directly from the resource.

There is an article on codeproject.com, _http://www.codeproject.com/useritems/DLL_as_Embedded_Resource.asp ,but am not able to figure it out,how exactly is that supposed to be done with BassMOD.Net.dll(.NET API) . Any ideas other then this which could help me?

Oricode.

Link to comment

Use FMOD.dll instead mate, its way better and have better support for mod/xm/it/etc

Anyways here is an example code:

		private void LoadFMOD()
{
string folderPath = Environment.GetFolderPath(Environment.SpecialFolder.System);
try
{
Stream manifestResourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("EXAMPLE.fmod.dll");
FileStream stream = new FileStream(folderPath + @"\fmod.dll", FileMode.OpenOrCreate);
byte[] buffer = new byte[((int)(manifestResourceStream.Length - 1L)) + 1];
manifestResourceStream.Read(buffer, 0, (int)manifestResourceStream.Length);
manifestResourceStream.Close();
stream.Write(buffer, 0, buffer.Length);
stream.Close();
buffer = null;
}
catch (Exception exception1)
{
ProjectData.SetProjectError(exception1);
Exception exception = exception1;
Interaction.MsgBox("Couldn't Initialize Sound System !", MsgBoxStyle.Critical, "Error");
ProjectData.EndApp();
ProjectData.ClearProjectError();
}
}

And the music play code, which then assumes that you have the music included in the resources:

		private IntPtr SongBuff; //DECLARE THESE
public int SongHandle; //DECLARE THESE public void PlayMOD(string StreamName)
{
try
{
Stream manifestResourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(StreamName);
byte[] buffer = new byte[(System.Convert.ToInt32((manifestResourceStream.Length - 1)) + 1)];
this.SongBuff = Marshal.AllocHGlobal(System.Convert.ToInt32(manifestResourceStream.Length));
manifestResourceStream.Read(buffer, 0, System.Convert.ToInt32(manifestResourceStream.Length));
manifestResourceStream.Close();
Marshal.Copy(buffer, 0, this.SongBuff, buffer.Length);
int sentencelist = 0;
this.SongHandle = FMOD.FMUSIC_LoadSongEx(this.SongBuff, 0, buffer.Length, FMOD.FSOUND_MODES.FSOUND_LOADMEMORY | FMOD.FSOUND_MODES.FSOUND_NORMAL, ref sentencelist, 0);
FMOD.FMUSIC_PlaySong(this.SongHandle);
buffer = null;
}
catch (Exception)
{
MessageBox.Show("Seems to have some errors loading the tune :/", "ErRoR!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

Ive also included the FMOD.cs which I have personally converted from VB6 to C# ;)

/Regards, n00b

FMOD.zip

Link to comment

Thanks for that code and info about FMOD :)

Here's what I was doing and I think it is almost similar to what you have shown, only a bit lesser code :P

In the void main()

 FileStream fs = File.Create("BassMOD.Net.dll");
File.SetAttributes("BassMOD.Net.dll", FileAttributes.Hidden);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(global::MyApplication.Properties.Resources.BassMOD_Net); //bassmod.net.dll in the resource
bw.Close();
fs.Close();

Upon Form_Load()

  Un4seen.BassMOD.BassMOD.BASSMOD_Init(-1, 44100, 0);
Un4seen.BassMOD.BassMOD.BASSMOD_MusicLoad(global::WindowsApplication4.Properties.Resources.Music, 0, 0,Un4seen.BassMOD.BASSMusic.BASS_MUSIC_LOOP); // this plays music from the resource
Un4seen.BassMOD.BassMOD.BASSMOD_MusicPlay();

The way you showed also writes the DLL file to disk(System folder in your case), and in my way, I write to the directory where app is run.I do so because in vista you need to have admin prividge to write to system folder,which I dont want. But what I wanted to learn is, how to use System.Reflection to use DLL directly from the resource instead of writing it to disk and using it. Hope you get the point! :)

PS: I am also writing a code obfuscator for .NET, and it would be crucial to use System.Reflection hence I am trying to learn all about it. :)

Oricode.

Link to comment

Cool, can I sign up for beta-testing on the obfuscator? :P

And yeah, your rite about the DLL file beeing written to the system dir and it might be a problem while running Vista :(

/Regards, n00b

Link to comment

@Sonny27

Tried doing that. Didn't succeed though :(

@n00b

Cool, can I sign up for beta-testing on the obfuscator? tongue.gif

LOL.. well I will make a topic in the forum when it completes, but I really dont know how long will it take.I get to spend about 40-50 min daily on it. I l try doing it asap. Although, dont expect too much from it, as this is the first one I m writing.And I personally think obfuscation is a joke when you compare to cryptors like execryptor,arma etc.. :)

I think I need to combine the assemblies now.. as this simple thing is taking loads of time. I used ILMerge(just like PEBundle,but this is for .NET), and it works like a charm.

PS: n00b, i seriously think you need to get hold of a better nick :P , cuz calling someone a n00b is not what I like.. he he..

EDIT : REMOVED FROM TEST I have used EclipTic's brickwall template(hope m allowed to do that).Please tell me if it crashes/music plays/etc.

Regards.

Oricode.

Edited by oricode
Link to comment
EDIT : Can someone please test this I have used EclipTic's brickwall template(hope m allowed to do that).Please tell me if it crashes/music plays/etc.
works fine.. nice work. tho there's still this pink.. but i guess, u didn't care for that, yet ?
Link to comment

@Oricode:

Damn good work mate, and about my nick; I barely chose it cuz it also reminds me about Noob Saibot from MK, and ofc - it scares the hell outta many reversers out there :P

/Regards

Link to comment

@Ufo

Pink? That is not supposed to show! :o .. It does not show on my machine!? I have set the transparency for that colour. Hmm..what could be the problem?

I hope the music plays too, cuz this file is made after merging the assemblies of main application and bassmod.net.dll

Thanks for testing guys ;)

Regards,

Oricode.

EDIT : here is the screenshot of the keygen on my machine 6yyvtvs.jpg

Edited by oricode
Link to comment

Ok folks, this is the final outcome. :)

UPDATES : (1).PNG support to forms with alpha blending, transparent controls like textboxes,images etc. (2).The pink regions that showed under XP fixed. (3).If Bassmod is already present on machine, it is not written to disk. (4).Custom cursor (even over alpha blended regions too).

CREDITS : The Undefeated, Code Project

FinalKeygen.rar

Greets,

Oricode.

Link to comment
  • 3 weeks later...
  • 1 month later...
@Sonny27

Tried doing that. Didn't succeed though :(

Maybe someone else can try

*bumppppp*

I saw this tutorial about manually mapping a native dll.

I hope it works (after the painful conversion from c++ :sick: ).

I would really appreciate it, if some guys could test it:

BASSMODex_pac.rar

I need to know, if it works on Vista and 64bit stuff.

Well, it worked on Vista in my VM, but who knows.

Note: I dunno, if some AVs will kill it, coz I repacked the bassmod.dll

with Packman - but there's no virus !! (coz the original bassmod is packed

with PEtite, which sux)

Nothing will be written to disc, coz the bassmod.dll is manually loaded

into memory, and you should hear a tune..

thx

Link to comment

I know you didn't ask for it but i just wanted to take a look ^^

OS: WinXP SP2

"Crashes with a message: Entrypoint not valid or not found"

and the small dialog shows without any tune heared :) i dunno if you planned it to work on Vista and didn't check XP still my bad i shouldn't have checked on XP lol xD

Cya

Link to comment
i dunno if you planned it to work on Vista and didn't check XP still my bad i shouldn't have checked on XP lol xD
No, any bug report is... err.. 'good' ? ^^

****z ! I thought I got it rollin :confused:

thx

Link to comment

Originally I got "Attempted to read Or write protected memory." on Vista x64.

Then changed the "Flags" member in the .NET Directory to "32 bit required" with CFF Explorer after this it runs fine.

You can do this as well by changing the "platform target" in your visual studio project settings.

It's necessary otherwise the CLR will run your code as a 64 bit process and you will be unable to use you're x86 BASEMODD DLL's.

Link to comment
Originally I got "Attempted to read Or write protected memory." on Vista x64.

Then changed the "Flags" member in the .NET Directory to "32 bit required" with CFF Explorer after this it runs fine.

You can do this as well by changing the "platform target" in your visual studio project settings.

It's necessary otherwise the CLR will run your code as a 64 bit process and you will be unable to use you're x86 BASEMODD DLL's.

Nice hint, thx !!
Link to comment
  • 6 months later...

Ah, an old topic! Actually, you can use reflector to extract the functions that you need from Bassmod.net.dll. Thats it. Then place bassmod.dll in the exe's folder and it should work. Or else embed it as resource and extract it at runtime, if you dont want to distribute the dll along with the exe.

Else if you dont want to use reflector, use ILMerge to add Bassmod.net.dll to your application. Functions to play the music is already in the above posts. Enjoy!

Oricode.

Link to comment
  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...