I am using EnumBuilder like described in https://stackoverflow.com/a/792332/910502 to create an enum based on values of a database and like'd to use this approach on a non-developer machine - I can deploy a dummy assembly, but have no post-build events and can't trigger the console app, because the assembly is already in use by the exe referencing it and therefore the access is denied.
One approach might be to have a third app, that functions as a starter:
First start the console app that creates the assembly, then start the application using it.
What is the recommended way for situations like this?
You didn't provide much details, but I think that you can achieve your goal by using simple bash/powershell script which will run 'enum-generator' and then 'app' which will expect this dll at specyfic directory.
Regarding dynamic enum generator. Have you considered using T4 Templates? Looks like a little bit cleaner approach.
Related
I’m creating a class library DLL which will be used by others. I have no knowledge of what that project will be, what the project name will be and etc. When it’s all said and done, all I will be doing is handing them the DLL for any project to reference.
The problem now is that I need a way for my DLL to be able to read and load key/values from the project at runtime. The project will pass the key/values to my DLL and said DLL will do the reading and writing of these key/values. I’ve found out that there are multiple ways to do that:
1) User settings from Settings/Appconfig
2) Create a new XML file to read and write from
However both ways are not automated. Correct me if I’m wrong:
For 1) The project side will have to manually create a Settings file with the key/values my DLL needs.
For 2) The project side will have to manually create an XML file with the key/values my DLL needs. Plus my DLL will need to know where that XML is being saved by the project referencing it (so that I can read and write from it from the DLL)
This is where I need help. Is there a way I can achieve reading and writing values from my DLL at runtime without the project having to do any manual work? And if not, what would be the best approach to go about this? Hand over a Settings file to the people who will be referencing my DLL?
Thanks!
Well, the client (the project using your dll) will have to do something in order to use you dll, right.
Your choice is to either define a way of configuration or to change your interface, so that the caller can provide the key-value pairs directly. You can still use app-settings from the consuming application's configuration-file. Your DLL can access them by using System.Configuration.ConfigurationManagerwithout having to know anything about where the application is running.
The only thing the other project would have to do is to provide the settings you need in the config.
But maybe you could elaborate a little more on what you are actually trying to do, "reading values" is not very specific.
I have an xml file that I want to include along with my program as a template. I would prefer that it be bundled with the .exe when the project is completed.
Is this possible?
If so, how should I reference it in the code? I would assume that referencing "myXML.xml" won't work because, if it is included, the file no longer exists as a standalone object.
An alternative idea is to copy/paste the contents into string, but that seems like a bad idea in so many ways. (It's 900 lines.)
Ideas?
Thank you.
The real motivation behind this is I really prefer standalone executables rather than making the user go through the installation process. Additionally there's the extra benefit that they're less likely to f- it up.
This is quite simple to do, you can store files in any .NET assembly as "Embedded Resources", which can be then accessed at runtime.
See the Microsoft article here for a detailed rundown on how to do this.
Another way is resource files, http://msdn.microsoft.com/en-us/library/7k989cfy%28v=vs.80%29.aspx
I am not sure the best way to explain this so please leave comments if you do not understand.
Basically, I have a few libraries for various tasks to work with different programs - notification is just one example.
Now, I am building a new program, and I want it to be as lightweight as possible. Whilst I would like to include my notification engine, I do not think many people would actually use its functionality, so, I would rather not include it by default - just as an optional download.
How would I program this?
With unmanaged Dlls and P/Invoke, I can basically wrap the whole lot in a try/catch loop, but I am not sure about the managed version.
So far, the best way I can think of is to check if the DLL file exists upon startup then set a field bool or similar, and every time I would like a notification to be fired, I could do an if/check the bool and fire...
I have seen from the debug window that DLL files are only loaded as they are needed. The program would obviously compile as all components will be visible to the project, but would it run on the end users machine without the DLL?
More importantly, is there a better way of doing this?
I would ideally like to have nothing about notifications in my application and somehow have it so that if the DLL file is downloaded, it adds this functionality externally. It really is not the end of the world to have a few extra bytes calling notification("blabla"); (or similar), but I am thinking a lot further down the line when I have much bigger intentions and just want to know best practices for this sort of thing.
I do not think many people would
actually use its functionality, so, I
would rather not include it by default
- just as an optional download.
Such things are typically described as plugins (or add-ons, or extensions).
Since .NET 4, the standard way to do that is with the Managed Exensibility Framework. It is included in the framework as the System.ComponentModel.Composition assembly and namespace. To get started, it is best to read the MSDN article and the MEF programming guide.
You can use System.Reflection.Assembly and its LoadFile method to dynamically load a DLL. You can then use the methods in Assembly to get Classes, types etc. embedded in the DLL and call them.
If you just check if the .dll exists or load every .dll in a plugin directory you can get what you want.
To your question if the program will run on the user's machine without the dlls already being present - yes , the program would run. As long as you dont do something that needs the runtime to load the classes defined in the dll , it does not matter if the dll is missing from the machine. To the aspect you are looking for regarding loading the dll on demand , I think you are well of using some sort of a configuration and Reflection ( either directly or by some IoC strategy. )
Try to load the plugin at startup.
Instead of checking a boolean all over the place, you can create a delegate field for the notification and initialize it to a no-op function. If loading the plugin succeeds, assign the delegate to the plugin implementation. Then everywhere the event occurs can just call the delegate, without worrying about the fact that the plugin might or might not be available.
Is there a built-in way to determine if a test is running on a dev machine (within Visual Studio) or on a builder (within the MSTest runner)? If there isn't has anyone found a good solution to this?
I'm using data driven tests and I want to be able to filter the data source on local dev machines, using a home brewed UI, but I want the builder to run all the tests.
I've considered #if but that seems hacky. Any better solutions?
I'm successfully using Environment variables and conditional compilation. MSBuild can easily translate environment variables into preprocessor symbols which you can use in your code. Your MSBuild file must include this translation like this:
<PropertyGroup Condition="'$(ENVIRONMENT_VARIABLE)' != '' ">
<DefineConstants>$(DefineConstants);ENVIRONMENT_VARIABLE</DefineConstants>
</PropertyGroup>
What this snipped does is checking for the presence of ENVIRONMENT_VARIABLE and then then appends that variable to the existing DefineConstants list that indicates to MSBuild which symbols to define for compilation.
Defining an environment variable only on your build server/ or only on your dev boxes (depending on what's easier) is a very lean and flexible way to achieve simple configuration. If you need more advanced strategies, config files may be the way to go. But be careful with introducing different build combinations, usually they create a lot of overhead and introduce chance to break the build accidentially.
Whenever you can, avoid it.
When I've had to make unit tests behave differently on the build machine as opposed to dev machines, I've ended up using Environment.MachineName to determine whether the machine's the build server or not. Don't know if that'll be any use to you.
Whatever you do, I'd just make sure it's well documented so that your colleagues know about it.
You can use any number of local settings on the dev box that won't be present in the official build or test boxes, and read those to determine if you need to switch to dev-box specific behavior. For example, a file, an XML File, a registry key, a preprocessor directive (as you mentioned).
As Robert Harvey suggested, application settings are another way to do it.
You can use an application setting.
Ok, so I was wondering how one would go about creating a program, that creates a second program(Like how most compression programs can create self extracting self excutables, but that's not what I need).
Say I have 2 programs. Each one containing a class. The one program I would use to modify and fill the class with data. The second file would be a program that also had the class, but empty, and it's only purpose is to access this data in a specific way. I don't know, I'm thinking if the specific class were serialized and then "injected" into the second file. But how would one be able to do that? I've found modifying files that were already compiled fascinating, though I've never been able to make changes that didn't cause errors.
That's just a thought. I don't know what the solution would be, that's just something that crossed my mind.
I'd prefer some information in say c or c++ that's cross-platform. The only other language I'd accept is c#.
also
I'm not looking for 3-rd party library's, or things such as Boost. If anything a shove in the right direction could be all I need.
++also
I don't want to be using a compiler.
Jalf actually read what I wrote
That's exactly what I would like to know how to do. I think that's fairly obvious by what I asked above. I said nothing about compiling the files, or scripting.
QUOTE "I've found modifying files that were already compiled fascinating"
Please read and understand the question first before posting.
thanks.
Building an executable from scratch is hard. First, you'd need to generate machine code for what the program would do, and then you need to encapsulate such code in an executable file. That's overkill unless you want to write a compiler for a language.
These utilities that generate a self-extracting executable don't really make the executable from scratch. They have the executable pre-generated, and the data file is just appended to the end of it. Since the Windows executable format allows you to put data at the end of the file, caring only for the "real executable" part (the exe header tells how big it is - the rest is ignored).
For instance, try to generate two self-extracting zip, and do a binary diff on them. You'll see their first X KBytes are exactly the same, what changes is the rest, which is not an executable at all, it's just data. When the file is executed, it looks what is found at the end of the file (the data) and unzips it.
Take a look at the wikipedia entry, go to the external links section to dig deeper:
http://en.wikipedia.org/wiki/Portable_Executable
I only mentioned Windows here but the same principles apply to Linux. But don't expect to have cross-platform results, you'll have to re-implement it to each platform. I couldn't imagine something that's more platform-dependent than the executable file. Even if you use C# you'll have to generate the native stub, which is different if you're running on Windows (under .net) or Linux (under Mono).
Invoke a compiler with data generated by your program (write temp files to disk if necessary) and or stored on disk?
Or is the question about the details of writing the local executable format?
Unfortunately with compiled languages such as C, C++, Java, or C#, you won't be able to just ``run'' new code at runtime, like you can do in interpreted languages like PHP, Perl, and ECMAscript. The code has to be compiled first, and for that you will need a compiler. There's no getting around this.
If you need to duplicate the save/restore functionality between two separate EXEs, then your best bet is to create a static library shared between the two programs, or a DLL shared between the two programs. That way, you write that code once and it's able to be used by as many programs as you want.
On the other hand, if you're really running into a scenario like this, my main question is, What are you trying to accomplish with this? Even in languages that support things like eval(), self modifying code is usually some of the nastiest and bug-riddled stuff you're going to find. It's worse even than a program written completely with GOTOs. There are uses for self modifying code like this, but 99% of the time it's the wrong approach to take.
Hope that helps :)
I had the same problem and I think that this solves all problems.
You can put there whatever code and if correct it will produce at runtime second executable.
--ADD--
So in short you have some code which you can hard-code and store in the code of your 1st exe file or let outside it. Then you run it and you compile the aforementioned code. If eveything is ok you will get a second executable runtime- compiled. All this without any external lib!!
Ok, so I was wondering how one would
go about creating a program, that
creates a second program
You can look at CodeDom. Here is a tutorial
Have you considered embedding a scripting language such as Lua or Python into your app? This will give you the ability to dynamically generate and execute code at runtime.
From wikipedia:
Dynamic programming language is a term used broadly in computer science to describe a class of high-level programming languages that execute at runtime many common behaviors that other languages might perform during compilation, if at all. These behaviors could include extension of the program, by adding new code, by extending objects and definitions, or by modifying the type system, all during program execution. These behaviors can be emulated in nearly any language of sufficient complexity, but dynamic languages provide direct tools to make use of them.
Depending on what you call a program, Self-modifying code may do the trick.
Basically, you write code somewhere in memory as if it were plain data, and you call it.
Usually it's a bad idea, but it's quite fun.