Equivalence of CreateObject("Microsoft.XMLHTTP") - c#

I got some code from our provider which is written in VB. I need this code to be converted to C#.
Below if the VB code i need to convert to C#.
Dim VLINK_URL As String
VLINK_URL = "http://service.xxx.com/report?type=" & REPORT_TYPE & "&vin=" & vinNumber
Dim GetConnection As Object
GetConnection = CreateObject("Microsoft.XMLHTTP")
GetConnection.Open("get", VLINK_URL, False, USERID, PASS)
GetConnection.Send()
Where USERID and PASS are properties of the class to set the credentials.
The exact problem is with CreateObject("Microsoft.XMLHTTP").
Can somebody help me resolve this issue.
Thanks for taking your valuable time.

Look up WebRequest.
See examples section.

Use Type.GetTypeFromProgID:
Type xmlType = Type.GetTypeFromProgID("Microsoft.XMLHTTP");
dynamic objXML = Activator.CreateInstance(objBLType);
objXML.Open("get", VLINK_URL, False, USERID, PASS)
objXML.Send()
However, I would take a step back and understand what you're trying to do - the XMLHTTP COM library is at nearly 15 years old now and existed before .NET had better support for web serivces (assuming that's what you're using the library for).

Related

How to call API using C#?

I am not a c# guy. Needless to say that I don't have experience on this topic.
I bought a software and I installed it on my computer. Now I thought of using some of it's functions on my software(that I make to plan in c#). So I messaged the person who sold me the software and he send me a two page pdf file explaining what to do.
It states:
This software features a COM interface.
And it goes saying it's API contains a function "stackAPI".
and the parameters used are apiname type string, apipass type string.
Return values type long. 0 for sucess and 1 for error.
That's all it states, I tired searching google, it could not help me at all. So how do I start?
when I write the following code on c# it gives me error.
string[] apiname;
string[] apipass;
stackAPI(apiname, apipass);
I know if I was using dll I would import it as
[DllImport("example.dll"]
But no dll is provided.
Do I need to add the path to the folder where the software is installed to call the API ?
To get started:
using example.dll;
Then in your main class:
example.CustomService api = new example.CustomService();
var response = api.Dostuff();
Console.WriteLine(response);
If anyone wants to know how I did it. After a week of searching I was able to find the solution today. I wanted to call the function stackAPI(apiname, apipass);.
stackAPI(apiname, apipass) was the member of stack.callapi
So I wrote:
dynamic lifestohack = Activator.CreateInstance(Type.GetTypeFromProgID("stack.callapi"));
than just you can call the function like this.
int rv;
string[] apiname;
string[] apipass;
rv=lifestoahck.stackAPI(apiname, apipass);
Hope it may help someone.

Access Twitter API from VB.NET

I'm using a python code to make some experiments on Twitter data. My code is something like this (in Python):
import twitter;
CONSUMER_KEY='xxxx';
CONSUMER_SECRET='xxxx';
OAUTH_TOKEN = 'xxxx';
OAUTH_TOKEN_SECRET = 'xxxx';
auth = twitter.oauth.OAuth(OAUTH_TOKEN, OAUTH_TOKEN_SECRET,CONSUMER_KEY, CONSUMER_SECRET);
twitter_api = twitter.Twitter(auth=auth);
q = 'mysearch';
count = 15;
search_results = twitter_api.search.tweets(q=q, count=count, result_type='popular');
What is the best way to have the same functionalities in VB.NET (or C#)? How can I traslate the main functionalities of Twitter Python Package (like twitter.oauth.OAuth function, for ex.)?
Thank you!
There is no 'best way' to convert from python to .net. Some libraries are ready for the twitter platform, you have many options available at Twitter Docs, you will need to use one of them (or write you own ... xD)

QuickBooks Intuit IPP Issue Check

I am attempting to issue a check to a vendor through the Intuit.IPP API (Interop.QBFC12) using the following code:
IMsgSetRequest messageSet = session.CreateMsgSetRequest("US", 7, 0);
ICheckAdd cheque = messageSet.AppendCheckAddRq();
cheque.AccountRef.ListID.SetValue(vendor.ListID.GetValue());
cheque.AccountRef.FullName.SetValue("myAccountName");
cheque.TxnDate.SetValue(DateTime.Today);
IMsgSetResponse responseSet = session.DoRequests(messageSet);
IResponse response = responseSet.ResponseList.GetAt(0);
responseType = (ENResponseType)response.Type.GetValue();
if (responseType == ENResponseType.rtCheckAddRq)
returnMessage = response.StatusMessage;
The resulting response.StatusMessage = "Object 80000005-1374598713 specified in the request cannot be found. " The object ID specified is the ListID I pulled for the vendor.
I'm coding pretty much blind as I'm not finding the IPP documentation very helpful or up to date for version 12.
What am I missing?
This:
cheque.AccountRef.ListID.SetValue(vendor.ListID.GetValue());
cheque.AccountRef.FullName.SetValue("myAccountName");
Is what's causing you the problem.
The AccountRef node is for specifying an Account, not a Vendor. An Account is something from the QuickBooks Chart of Accounts (an "Account" object). A Vendor is something from the vendor list (a "Vendor" object) and they are not interchangable objects.
Since you're already setting an AccountRef/FullName:
cheque.AccountRef.FullName.SetValue("myAccountName");
You don't need to set a AccountRef/ListID.
You should always either set a ListID or a FullName - there's no reason to set both (it's just alternative ways to reference the same object - both are "foreign keys" of sorts to QuickBooks).
What you probably meant to do is set the AccountRef like you meant to, but also set the PayeeEntityRef. For example:
cheque.PayeeEntityRef.ListID.SetValue(vendor.ListID.GetValue());
Also, regarding this:
I'm coding pretty much blind as I'm not finding the IPP documentation
very helpful or up to date for version 12.
IPP is an entirely different API from what you're using. The IPP docs aren't going to be useful to you, because it's not anywhere close to the API you're using.
What you should be using for reference is the QuickBooks OSR:
https://member.developer.intuit.com/qbSDK-current/Common/newOSR/index.html
And also the QuickBooks SDK downloadable .EXE, which has a bunch of relevant PDF documentation.

C# code completion with NRefactory 5

I just found out about NRefactory 5 and I would guess, that it is the most suitable solution for my current problem. At the moment I'm developing a little C# scripting application for which I would like to provide code completion. Until recently I've done this using the "Roslyn" project from Microsoft. But as the latest update of this project requires .Net Framework 4.5 I can't use this any more as I would like the app to run under Win XP as well. So I have to switch to another technology here.
My problem is not the compilation stuff. This can be done, with some more effort, by .Net CodeDomProvider as well. The problem ist the code completion stuff. As far as I know, NRefactory 5 provides everything that is required to provide code completion (parser, type system etc.) but I just can't figure out how to use it. I took a look at SharpDevelop source code but they don't use NRefactory 5 for code completion there, they only use it as decompiler. As I couldn't find an example on how to use it for code completion in the net as well I thought that I might find some help here.
The situation is as follows. I have one single file containing the script code. Actually it is not even a file but a string which I get from the editor control (by the way: I'm using AvalonEdit for this. Great editor!) and some assemblies that needs to get referenced. So, no solution files, no project files etc. just one string of source code and the assemblies.
I've taken a look at the Demo that comes with NRefactory 5 and the article on code project and got up with something like this:
var unresolvedTypeSystem = syntaxTree.ToTypeSystem();
IProjectContent pc = new CSharpProjectContent();
// Add parsed files to the type system
pc = pc.AddOrUpdateFiles(unresolvedTypeSystem);
// Add referenced assemblies:
pc = pc.AddAssemblyReferences(new CecilLoader().LoadAssemblyFile(
System.Reflection.Assembly.GetAssembly(typeof(Object)).Location));
My problem is that I have no clue on how to go on. I'm not even sure if it is the right approach to accomplish my goal. How to use the CSharpCompletionEngine? What else is required? etc. You see there are many things that are very unclear at the moment and I hope you can bring some light into this.
Thank you all very much in advance!
I've just compiled and example project that does C# code completion with AvalonEdit and NRefactory.
It can be found on Github here.
Take a look at method ICSharpCode.NRefactory.CSharp.CodeCompletion.CreateEngine. You need to create an instance of CSharpCompletionEngine and pass in the correct document and the resolvers. I managed to get it working for CTRL+Space compltition scenario. However I am having troubles with references to types that are in other namespaces. It looks like CSharpTypeResolveContext does not take into account the using namespace statements - If I resolve the references with CSharpAstResolver, they are resolved OK, but I am unable to correctly use this resolver in code completition scenario...
UPDATE #1:
I've just managed to get the working by obtaining resolver from unresolved fail.
Here is the snippet:
var mb = new DefaultCompletionContextProvider(doc, unresolvedFile);
var resolver3 = unresolvedFile.GetResolver(cmp, loc); // get the resolver from unresolvedFile
var engine = new CSharpCompletionEngine(doc, mb, new CodeCompletionBugTests.TestFactory(resolver3), pctx, resolver3.CurrentTypeResolveContext );
Update #2:
Here is the complete method. It references classes from unit test projects, sou you would need to reference/copy them into your project:
public static IEnumerable<ICompletionData> DoCodeComplete(string editorText, int offset) // not the best way to put in the whole string every time
{
var doc = new ReadOnlyDocument(editorText);
var location = doc.GetLocation(offset);
string parsedText = editorText; // TODO: Why there are different values in test cases?
var syntaxTree = new CSharpParser().Parse(parsedText, "program.cs");
syntaxTree.Freeze();
var unresolvedFile = syntaxTree.ToTypeSystem();
var mb = new DefaultCompletionContextProvider(doc, unresolvedFile);
IProjectContent pctx = new CSharpProjectContent();
var refs = new List<IUnresolvedAssembly> { mscorlib.Value, systemCore.Value, systemAssembly.Value};
pctx = pctx.AddAssemblyReferences(refs);
pctx = pctx.AddOrUpdateFiles(unresolvedFile);
var cmp = pctx.CreateCompilation();
var resolver3 = unresolvedFile.GetResolver(cmp, location);
var engine = new CSharpCompletionEngine(doc, mb, new CodeCompletionBugTests.TestFactory(resolver3), pctx, resolver3.CurrentTypeResolveContext );
engine.EolMarker = Environment.NewLine;
engine.FormattingPolicy = FormattingOptionsFactory.CreateMono();
var data = engine.GetCompletionData(offset, controlSpace: false);
return data;
}
}
Hope it helps,
Matra
NRefactory 5 is being used in SharpDevelop 5. The source code for SharpDevelop 5 is currently available in the newNR branch on github. I would take a look at the CSharpCompletionBinding class which has code to display a completion list window using information from NRefactory's CSharpCompletionEngine.

How do I get latest clearcase label programmatically from C#?

I am trying to put version information to my C# GUI framework retrieved from the latest ClearCase label. This was originally done from Visual Soursafe as below.
vssDB = new VSSDatabaseClass();
vssDB.Open( databaseName, "vssadmin", "vssadmin" );
VSSItem item = vssDB.get_VSSItem( #"$\BuildDCP.bat", false );
foreach(VSSVersion vssVersion in item.get_Versions(0))
{
// Pull the first non-blank label and use that
if ( vssVersion.Label != "" )
{
labelID = vssVersion.Label.ToString();
break;
}
}
I am trying to do something similar using ClearCase since we changed our source code control from VSS to CC. Any help would be greatly appreciated.
Thanks!
I believe this could be better achieved through a script, which would be called from your C# program.
But you may be able to directly call some COM objects, through the CAL interface provided with ClearCase.
The documentation for the interface can be accessed through ClearCase help (Start>Programs>Rational ClearCase>ClearCase Help), where there's an entry for "ClearCase Automation Library (CAL)". An alternate path is to look in the ClearCase/bin directory for "cc_cal.chm".
In VB, with CAL API, that would give something like:
Dim CC As New ClearCase.Application
Dim labelID As String
Set aVersion = CC.Version("[Path-To]\BuildDCP.bat");
Set someLabels = Ver.Labels;
If (someLabels.Count > 0) Then
' the first label listed is the most recently applied
labelID = someLabels.Item(1).Type.Name
EndIf
I really wish that the COM interfaces had better documentation, or were more obvious. Or that the code to ClearCase Explorer or Project Explorer were open source.
I've done a few cool things, but I pretty much started by adding COM references to my C# project, and then started screwing around with the interfaces I found.
Good luck!

Categories

Resources