Cannot create ADODB.Connection with complicated programming chain - c#

Ok so this is a really complicated problem, I will try my best to explain.
We have a c++ application which communicates with Sql Connection like so:
CoCreateInstance(_T("ADODB.Connection"))
This works fine.
We recently made all of our backend code into a Com Object. If I write a Vb program to load our Com Object and do some database operations everything works fine, CoCreateInstance(_T("ADODB.Connection"))
still works.
We use fitnesse for testing so I wrote a fixture that:
1) Takes a string of vb code input into an html page.
2) compiles the vb code
3) runs the vb code that uses our Com Object.
* fitnesse is a java application so the code path travels through Java as well.
Now when any operation touches the database the Com Object hits an exception. Uses message boxes, and removing code I narrowed the problem down to this line of code:
CoCreateInstance(_T("ADODB.Connection"))
normally the return code is 0, but with this chain of code calling code I get the return code: 800401F3 which says that it cannot find the object to load.
I am pulling my hair out trying to figure out whats going on. Any bit of insight would be greatly appreciated.

It is telling you that it cannot find the ProgId in the registry. That's not very healthy, it is a pretty standard component on any Windows install. Verify this, fire up regedit.exe and navigate to HKLM\Software\Classes\ADODB.Connection
If that is missing then you need to install the dbase providers on that machine. Download the MDAC 2.8 installer from Microsoft and run it. If it is not missing then you have a more mysterious problem, perhaps something to do with this being a 64-bit operating system. Look in HKLM\Software\Wow6432Node then. Get additional diagnostics by using the SysInternals' ProcMon tool to see what it is poking at in the registry.

As an alternative, you don't say whether your com "object" is a .dll. If it is, then make sure it is either "self-registering" or you'll need to run this at the command prompt.
regsvr32 myobject.dll
If it's an exe with COM objects, register the objects by running the program with the "/RegServer" command line option like this:
myobject.exe /RegServer
HTH

Related

C# application says "No" when executed

I have developed a small-ish C# console application (TextMatcher.exe) on my local development machine and now need to deploy it to the live environment. It references another class library which I developed which has generic functions, which I intend to use and improve in future console applications.
Ultimately this specific application will be executed from within an SSIS package, but for now I'm just trying to run it from cmd.
I kid you not that this is the actual output from the program:
E:/TextMatcher>TextMatcher.exe
No
E:/TextMatcher>
The computer literally says "No" and gives no further information. I have not included, anywhere in the program, to output the word "No", on any failure or otherwise.
Of course, it runs fine locally. I made sure I included the dll of the utility class library too. I have read other questions (here, here) about how to deploy console apps correctly, and have followed the advice.
NB: This is also proving to be quite hard to Google because of the use of the word "No" being fundamental to the problem...
EDIT - It seems to be working now... I simply copied over the files again from my local machine to the remote machine... I am trying to get it to break again so that I can figure out what on Earth happened, and until I do, I will not accept an answer so that people could maybe shed some more light onto it. Either way it is quite baffling.
There's a chance that someone has modified the Image File Execution Options registry setting on the server to launch a debugger automatically.
In short, examine the registry key HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\currentversion\image file execution options and see if there's an entry that matches the name of your executable.
Check whether you have installed the necessary Framework components,
i.e. the suiting Dot net framework. Application with 4.0 and installed 3.5
can cause very strange behaviour.
Try writing a very simple window application and start it on the deployment machine
(this will give you probably more help what is missing).
Check whether the needed Dlls (that you developed, e.g. your class library) are reachable for the console application. And check whether the right version of your Dll is matched!
Check the plattform settings in your console application. Do they match with
the machine where you want to run your application? (win64 and win32 mismatch)
If all of those do not help, try inspecting your executable on the deployment machine
for example with depends.net, checkasm, or similar.
Does your production environment have AppLocker running? I don't know if its output can be customized to output "No" on a command line. Applocker comes to my mind because you can use it to restrict a system to run only signed executables. If your Textmatcher executable is unsigned, it may get shut down immediately. If you have the ability, I'd be curious to see if signing your binary changes the behavior.

Using R(D)COM for integrating R with C#

I am trying to use R(D)Com interface. I have R 2.12.1 installed on machine. For using this interface in C#, I loaded rscproxy_1.3-1 package and then installed R_Scilab_DCOM3.0-1B5 on my machine. Also, I copied sciproxy.dll from Program Files\R(D)COM Server\Scilab to Program Files\R(D)COM Server\bin, as informed while installing the interface.
My Problem:
As a part of testing, I tried the code from blog post http://vvella.blogspot.com/2010/08/integrate-c-net-and-r-taking-best-of.html. But my form application failed due to exception raised by statement rconn.Init(“R”). The exception text was Exception from HRESULT: 0x80040013 I tried to run samples from Programs->R->R(D)COM Server->Server 01 Basic Test. On launched form, I clicked button “Start R” but it failed with error printed in text box as “Initializing R...Function call failed Code: -2147221485 Text: installation problem: unable to load connector”
I tried this:
I tried to troubleshoot it with the help of Index html page, and there under installation section, I found that there must be rproxy.dll under installed R/Bin folder. Also, HKEY_LOCAL_MACHINE\Software\R-core\R\InstallPath should point to installation folder.
Things lacking on my machine are
the installed R/bin folder doesn’t
contain rproxy.dll. Where can I get
this dll? Or is it sciproxy.dll
instead?
HKEY_LOCAL_MACHINE\Software\R-core\R\InstallPath
points to installation folder, but
there is no entry under
HKEY_CURRENT_USER\Software.
I can guess there is something fishy about installation, or registering COM server. But I am not successful in figuring it out.
Could you please tell me where am I going wrong?
thanks,
Kapil
Oh god I remember this being a huge pain in the arse. Lets see if I can remember... And before I start, I warn you that I just "got this working" and never cared to work out if I could remove parts from the process.
Downloads are available from http://rcom.univie.ac.at/download.html . If I remember correctly, the RandFriends package is all you need, it installs a crapload (just install it all) but is simple. Alternatively, I think if you install the 'rscproxy' package in R you can just download the 'statconnDCOM' and install that. Memory is hazy, but I know one of these methods results in an annoying splash screen everytime you run your C# executable, and one doesn't. Although that could have just been some setting I played with.
Now, I can't remember how you verify that stuff has installed successfully. Pretty sure it comes with examples though. Once that is started, get your C# project open. Reference the following projects,
StatConnectorCommonLib
STATCONNECTORSRVLib
In your code, you will probably want to implement a IStatConnectorCharacterDevice so you get the R output coming back out in C#. Your code to initialise will then look something like,
private StatConnector _StatConn;
private IStatConnectorCharacterDevice _CharDevice;
private Whatever()
{
// declare
_StatConn = new StatConnectorClass();
_CharDevice = new MyCharDevice();
// init R, wire up char device
_StatConn.Init("R");
_StatConn.SetCharacterOutputDevice(_CharDevice);
}
Then you should be able to just use the functions as needed
_StatConn.EvaluateNoReturn("x <- 3");
var returnObj = _StatConn.Evalute("1 + 1");
Hope that helps.
tl;dr download RAndFriends, do fresh install with that
I had a similar problem calling R.Init(), I found R.GetErrorText() returns the actual error message

How can I get data off of my Windows Phone 7 emulator

I have a unit testing framework for WP7 and it runs on the phone. The results are fairly hard to read so I am writing them to an XDocument.
My question is, how can I then get this XML file off of the phone and onto my desktop where I can actually analyze the results?
What I have done so far is to put a Debugger.Break() line right after where the summary xml is created. I can then copy/paste the xml out of VS or inspect it right in the debugger. The problem is though, that if you don't already have a debugger attached (which is good when lots of ExpectedException tests) Debugger.Attach() seems to not work, also manually attaching VS to the emulator processes seems to do nothing.
I tried running the emulator with some extra command line parameters so I could try to see if I could get it to use my actual hard drive as it's own disk but I couldn't seem to get it to work...
PS it's probably not reasonable to pop open a new process such as a webserver to listen for this data. I know how to do that, I would just rather not.
So how the heck do you get stuff off of these phones??
Have a look at this article about emulator automation from Justin Angel.
It includes details on how to remotely read and write files from/to emulator/device isolated storage.
As you pointed out the other alternative would be to have the applciation send the results to a [local] web server.
The article by Justin Angel is really great, but unfortunately his file-based solution does not work on the final RTM versions of the CoreCon API. Microsoft has simply removed that functionality from the native conman layer.
I've been in the same situation as you and have contemplated various ways to get data out of the device, but in the end only one thing seems to work: as you suggest yourself, pass data to an external webservice.
That solution is less than ideal not only because it takes some effort, but also because of a few caveats:
your app must be granted ID_CAP_NETWORKING capability
network-traffic seems disallowed in the Application_Closing event, and maybe elsewhere too
On the bright side I found that webrequests from the phone, both hardware and emulator, were really fast so the approach works very well (our app is EQATEC Profiler for WP7).

SharePoint fails to load a C++ DLL on Windows 2008

I have a SharePoint DLL that does some licensing things and as part of the code it uses an external C++ DLL to get the serial number of the hardisk.
When I run this application on Windows Server 2003 it works fine, but on Windows Server 2008 the whole site (loaded on load) crashes and resets continually. This is not Windows Server 2008 R2 and is the same in 64 or 32 bits.
If I put a Debugger.Break before the DLL execution then I see the code get to the point of the break and then never come back into the DLL again. I do get some debug assertion warnings from within the function, again only in Windows Server 2008, but I'm not sure this is related.
I created a console application that runs the C# DLL, which in turn loads the C++ DLL, and this works perfectly on Windows Server 2008 (although it does show the assertion errors, but I have suppressed these now).
The assertion errors are not in my code but within ICtypes.c, and not something I can debug.
If I put a breakpoint in the DLL it is never hit and the compiler says:
"step in: Stepping over non user code"
If I try to debug into the DLL using Visual Studio.
I have tried wrapping the code used to call the DLL in:
SPSecurity.RunWithElevatedPrivileges(delegate()
But this also does not help.
I have the source code for this DLL so that is not a problem.
If I delete the DLL from the directory I get an error about a missing DLL. If I replace it, back to no error or warning just a complete failure.
If I replace this code with a hardcoded string the whole application works fine.
Any advice would be much appreciated, I can't understand why it works as a console application, yet not when run by SharePoint. This is with the same user account, on the same machine...
This is the code used to call the DLL:
[DllImport("idDll.dll", EntryPoint = "GetMachineId", SetLastError = true)]
extern static string GetComponentId([MarshalAs(UnmanagedType.LPStr)]String s);
public static string GetComponentId()
{
Debugger.Break();
if (_machine == string.Empty)
{
string temp = "";
id= ComponentId.GetComponentId(temp);
}
return id;
}
This could be security related:
An important point is that it works in a console app.
In a console app RunWithElevatedPrivileges has no effect since it emulates the app pool user for your worker process, a user that should have no special rights on the box itself.
In contrast a console app runs in context of the logged in user.
Try emulating a user with rights like when you run the console application specified here (with Undo() inside try/finally mind you!). When obtaining the token you can create an SPUserToken and establish site context using the SPSite constructor that takes a GUID and a SPUserToken
Theres several examples out there documenting this approach, here for example.
EDIT: oh and the reason it worked on 2003 could be that your app pool account had way too many rights ;-)
Why not use WMI to get the serial number of hard disk, thus avoids execution of unmanaged code. See this sample How to Retrieve the REAL Hard Drive Serial Number
That non-deterministic crashing behavior is often seen with memory overwrites/corruption; sometimes it matters (crash), sometimes you get lucky.
You might want to check into getting a crash dump and analyze it with WindDbg. Since you have the source you could re-build it with the various stack, heap memory protection and warning systems enabled (depending on your compiler) and see what you get.
I'd find out if it is a User Account Control related problem, you can try to disable it.
2003 doesn't have UAC.
Your app pool account might not have the right to retrieve this information?
In visual studio, go into the properties of your executable assembly, and under the debug tab, check the enable debugging unmanaged code option.
If the method your are importing belongs to a class, you need to add the mangled C++ name (e.g. 2#MyClass#MyMethod?zii) as an entry point to the DllImport attribute (run depends on the native DLL to get it).
You do not need C++ for that: http://www.codeproject.com/KB/cs/hard_disk_serialno.aspx
If i put a breakpoint in the DLL it is
never hit and the compiler says :
"step in: Stepping over non user code"
That's the debugger, not the compiler, and if you configured it properly it wouldn't do that. Look for the options calls "Use native debugging" and "Just my code". The first one should be on and the second one off.
This problem may happen due to one of the problems listed below.
the web part may not have the right permissions to call the DLL or
you may not have set the appropriate trust level for your SharePoint site.
For the permission you can use impersonation and for the trust level below site can help you.
http://msdn.microsoft.com/en-us/library/dd583158(office.11).aspx
I made a new C++ DLL from scratch which works fine when referenced as a console application on Windows Server 2003 and Windows Server 2008, but as soon as I reference it from the DLL in SharePoint the same things happens and it won't run.
It does find the DLL, but I think it has no permissions to execute it, even if I put it into the My Documents section and reference it directly!

C# Console/CLI Interpreter?

I wonder if there is something like a standalone Version of Visual Studios "Immediate Window"? Sometimes I just want to test some simple stuff, like "DateTime.Parse("blah")" to see if that works. But everytime i have to create a new console application, put in my code and test it.
The Immediate Window sadly only works when I am debugging something. Could PowerShell do that? Just open a CLI similar to what cmd.exe does, allowing me to execute some C# code?
Linqpad - I use it like this all the time. http://www.linqpad.net/
Don't be misled by the name - that just describes the original motivation for it, not its functionality.
Just recently he released a version with proper statement completion - that's a chargeable add-on (the core tool is free), but a minute amount of money and well worth it, I think.
The Mono project includes an interactive C# shell, this may be just what you're looking for.
http://www.mono-project.com/CsharpRepl
C# Interactive window and csi.exe REPL were added to Visual Studio 2015 Update 1:
Introducing Interactive
The Interactive Window is back! The C# Interactive Window returns in Visual Studio 2015 Update 1 along with a couple other interactive treats:
C# Interactive. The C# Interactive window is essentially a read-eval-print-loop (REPL) that allows you to play and explore with .NET technologies while taking advantage of editor features like IntelliSense, syntax-coloring, etc. Learn more about how to use C# Interactive on Channel 9 or by reading our beginner’s walkthrough.
csi. If you don’t want to open Visual Studio to play around with C# or run a script file, you can access the interactive engine from the Developer Command Prompt. Type csi /path/myScript.csx to execute a script file or type simply csi to drop inside the command-line REPL.
Scripting APIs. The Scripting APIs give you the ability to execute snippets of C# code in a host-created execution environment. You can learn more about how to create your own C# script engine by checking out our code samples.
See What’s New in Visual Studio Update 1 for .NET Managed Languages.
Basically, now you have:
IDE REPL — C# Interactive window in VS
Script interpreter — csi foo.csx from Dev Cmd Prompt
Command line REPL — csi from Dev Cmd Prompt
Scripting API
Try scriptcs, it's not integrated into the VS IDE but it does let you type and run C# in a script window without the need for a project compiler etc...
Well, this isn't a direct answer to your question, but you could look at this tool:
Snippet Compiler
Also, if you want to see the IL produced, or similar, there is a tool that plugs into Reflector, called Snippy, based on the Snippy tool that Jon mentions in his own answer further down.
All of these are very nice to use.
As you suggest, PowerShell can do what you want. For example, to test your DateTime.Parse, the following one liner will do the trick:
PS C:\Documents and Settings\Dan> [System.DateTime]::Parse("Blah")
Exception calling "Parse" with "1" argument(s): "The string was not recognized as a valid DateTime. There is a unknown
word starting at index 0."
At line:1 char:25
+ [System.DateTime]::Parse( <<<< "Blah")
PS C:\Documents and Settings\Dan> [System.DateTime]::Parse("1/2/3")
01 February 2003 00:00:00
Note that the above uses the current release of PowerShell (v1.0). The next version of PowerShell will allows you to intermingle C# with PowerShell scripts more directly. To whet your appetite, watch this 7 minute screencast "C# to PowerShell" by Doug Finke. Very impressive!
If you're using Mono, there's this:
CsharpRepl
Don Box hacked something very simple up a few years ago too.
Along the lines of lassevk's answer, I've got "Snippy". This was developed for C# in Depth, and the UI is pretty rubbish, but it works - and lets you write extra members (methods, nested classes etc) as well, e.g.
public static void Foo()
{
Console.WriteLine("Hello");
}
...
Foo();
(The ... is used to tell Snippy "everything under here belongs in Main".)
We've just released CShell a full featured C# REPL IDE. It supports code completion, script files, adding references and is really extensible. Also we plan to add NuGet support soon, which will make it super quick to write some code and see how it works.
http://cshell.net/
We love LINQPad but it doesn't have a REPL, the code is executed once and you cannot do anything further with the results unless you modify the script and run the whole script again. This is okey, but sometimes if you want even more a scripty feeling then to evalute your code in a REPL is really nice and convenient.
I also find that SharpDevelop is so quick and lightweight that it is the easiest way to whip off a quick test project.
You may find the Object Test Bench useful. It's not very well known, but lets you create instances of classes, execute static methods and so on. It can be useful for discovering how to use unfamiliar APIs or for quick debugging of your own classes and methods, saving the creation of a test harness for simple checks.
You can find the MSDN documentation here:
http://msdn.microsoft.com/en-us/library/c3775d98%28VS.80%29.aspx
If you could wait a while.. it looks like there could be a C# equivalent of Ruby's irb in time for C# 4.0
Anders H. demonstrated an interactive console session where you could type in arbitrary C# code and see results in his 'Future of C#' piece at PDC 2008. You could even pop a WPF Window from it and then play with it via the console interface. Pretty cool.
Use LINQPad.
Name notwithstanding, it can execute any C# or VB code, from simple expressions to entire classes.
Plus, it can visualize entire object graphs in the results.
You can even add references to your own assemblies.
Try the C# REPL Script Environment that is part of the O2 Platform. It is exactly what you are asking for.
It will give you a perfect environment to try out all C# features and APIs (conceptually the O2 REPL environment is similar to LinqPAD)
You should also take a look at Roslyn from Microsoft. On Multiple Roslyn based tools (all running Stand-Alone outside VisualStudio) , the first one is a simple Roslyn REPL
If you happen to know and like Python, then IronPython may be a good alternative. It comes with a (Python) REPL console (ipy.exe) where you can import and use all of the .Net types.
I find it useful for testing out little things, exactly like DateTime.Parse("Blah").
Note that it can't actually execute C# code, but if all you want is access to .Net, then it's perfect. Also, if you install the IronPython Tools for VS, you can start a REPL session right in VS using a single keyboard shortcut (Alt+I) and leave it running in a docked window for when you need it.
Update: http://avillenas.com/post/interactive-c
Well, you can try this.
Download and install dotnet.github.io
Open cmd
Type "dotnet repl"
Type whatever you need.
I did what you wanted to do. Click here to see the gif
The Roslyn project sources contain a REPL called CSI (http://sourceroslyn.io/#csi/Csi.cs). The Csi class is currently internal but with the the “csi” project (Roslyn.sln: Interactive/Hosts/csi) an executable console application is available that supports e. g. the command #r to load an assembly and #load to load and execute script files (start the csi and use #help).

Categories

Resources