C# Revit API - How to pull info from autocad File - c#

I've been working on this for the past month and am trying to get the final connection.
so far, I can open Revit, execute an Autocad DLL from a Revit button however I don't know how to pull the information back into the Revit API. I have tried a couple of different methods however I've come to a wall. Here are the options I've tried with explanations below
1) Combine the DLL file and the Revit Button API file into one using classes.
---> Trying this method leaves me with an error when loading an existing CAD file. I don't get this error if I'm creating a new CAD file or if I'm running the DLL by itself with Autocad set to Debug.
Grabbing CAD file failed.
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
e.StackTrace:
at AcDbDatabase.{ctor}(AcDbDatabase* , Boolean , Boolean )
at Autodesk.AutoCAD.DatabaseServices.Database..ctor(Boolean buildDefaultDrawing, Boolean noDocument)
at CAD2Revit.AutoCadOperations.grabCAD(String filepath)
at CAD2Revit.Command.Execute(ExternalCommandData commandData, String& message, ElementSet elements)
when trying to run this line:
// Create a database and try to load the file
Autodesk.AutoCAD.DatabaseServices.Database db = new Database(false, true); // error occuring here often
I've done some research on the error and this is the only close repoo
2) Keep them separate
--> In this option, I've figured out how programatticly open AutoCAD, netload a DLL, and then execute it which grabs the file information but then I cant return the info to the Revit API
Any wisdom or advice is appreciated on how to approach this.
this is the closest post i've seen similar to this but its in VB i think and is over my head.
https://forums.autodesk.com/t5/net/system-accessviolationexception-attempted-to-read-or-write/td-p/4796397

I see no reason why such a problem should appear. If all else fails, you could always use IPC to separate the two processes. However, there should not really be any need for this.

Related

Intermittently getting exception that states "FileStream was asked to open a device that was not a file" in C# program

When calling File.WriteAllText to generate files, intermittently the c# windows form program will hit a catch block due to an Exception with the message of "FileStream was asked to open a device that was not a file. For support for devices like 'com1:' or 'lpt1:', call CreateFile, then use the FileStream constructors that take an OS handle as an IntPtr."
The stacktrace shows Microsoft.Win32.Win32Native.SafeCreateFile as being where it got to last.
After days of researching and trying to find out why this was happening, most articles talk about the reserved file names like com and etc. However, this exception is hit in the middle of exporting files after getting through a few files already. These files don't have any reserved names in them. They all have the same path except for at the end of the file name where a hyphen and padded number for which file it is on is added. Since this gets through similar named files, I believed that this wasn't what was actually happening. The other issue is I could do the same export process multiple times and not get it to happen every time. In fact out of 100 or so export processes it would maybe happen only once even though nothing in the name or path had changed.
Today I found a way to actually get it to happen almost every time. If I go to Chrome and open say "a.singlediv.com" to test with, start the export, then as the export is running I repeatedly reload the div site, it will 99% of the time halt the export and hit the exception mentioned above.
Does anyone have any advice that could lead me to why this Exception message is shown for doing this? Task Manager doesn't seem to have any glaring issues with Memory or CPU overload and a log is being posted to our SQL server after hitting the Exception so I also dont believe it is a network issue.
Update
I was able to find out more about when the error happens with testing yesterday. When refreshing the website repeatedly, I tried a couple different ways of exporting to get more information.
A custom configuration file, a text file, and an excel file are used as setup files for our program. I made identical folders on the Desktop and on a mapped drive which points to a folder located on our server for where the setup files are located as well as where the exports will be saved. I tried a couple different ways of loading these files and export locations.
If I load the setup files from the Desktop folder and save to the Desktop, or load from the Desktop folder and save to the mapped drive, or load from the mapped drive and save to the Desktop folder all of these don't seem to hit the Exception. So far the only way I have gotten the Exception to occur is when I load from the mapped drive and save to the mapped drive while doing the constant refreshing of the div website.
Attached below is a screenshot of the Exception.ToString() (Except for our custom methods that called File.WriteAllText() before in the StackTrace). I manually added returns between each method call so that it is easier to read.
Workaround Update
Per Mason's suggestion I wrapped my File.WriteAllText call in a Polly Retry which has seemed to so far allowed the export to finish every file while debugging the code.
I first did a RetryPolicy.Execute that wrapped the File.WriteAllText. Before File.WriteAllText I wrote the Attempt # it was on to the Output window and after it wrote a message about completing. I did get an Attempt #2 but was getting Attempt #1 and completed for every time it was called which was not easy to see since it happens over 2000 times. So I tried to add an if statement to check if this was not the first attempt and if it wasn't it would write the Attempt # and that the retry handled Exception to the Output window. However, after adding these if statements I didn't get any Attempt #s and it didn't break on the Exception but I also saw that the Output window did have a line about hitting the Exception so I assume that means that the Retry handled it? I would of expected to see Attempt #2 and Retry handled Exception around the Exception message in the Output window since it didn't stop and created every file needed. I have added a code snippet of what I added below.
int attempt = 0;
Polly.Retry.RetryPolicy retryIfException = Policy.Handle<System.NotSupportedException>().Retry(3);
retryIfException.Execute(() =>
{
if (attempt>0)
{
Console.WriteLine($"Attempt #{++attempt}");
}
File.WriteAllText(saveLocation, this.currentFileText.ToString());
if (attempt>1)
{
Console.WriteLine("Retry handled Exception");
}
});

how to get as400 spool files to text in c#

I'm trying to code a console application to get the spool file from as400 to a different destination as a text file. I have been looking around but I haven't found a small code to start from there, I been searching around and haven't found anything close.
You guys have any idea or suggestion on how to start this?
I have the same problem. I'm using the API calls QUSRTVUS to get the spooledfile list and want to open a specific spooled file with information of the previous API call.
After getting the file list I create a new userspace with QUSCRTUS. Then I want to open the file with QSPOPNSP but that is my problem.
Sourcecode to open a file
After opening the file I would read the content with QSPGETSP which copies the file in the userspace where I could read it.
That's the plan but without opening the file the rest doesn't work.
I get the error directly when calling the API, this is not an error message during the work of the call. Changing the parameters don't change the error message.
Maybe someone has a hint?
UPDATE: I found out the mistake. Despite the misleading error message, it was an authorization problem. I wasn't allowed to call the APIs.

Win API NTFS OpenFileByID throwing AccessViolationException

The OpenFileByID line in test() is giving me System.AccessViolationException Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
I am trying to replicate this code example (see the answer), which I'm running in Visual Studio Express 2013 for Windows Desktop. But this example doesn't seem to work for me. It is breaking on the OpenFileByID line in test().
In a nutshell, I am getting a file's ID, then trying to create a file handle from that ID. Later on I plan to use that handle to get information about the file. The reason I'm using IDs is so that I can repair broken links, since a target file's GUID is far more reliable than its presumed location. Help appreciated!
Edit: The file I'm trying to open is an ordinary text file on my Desktop, nothing special.
You're not checking to see if you got a valid volume handle - which you might not be. Could be the source of your a/v.
When you're opening the root dir, the doc says you shouldn't use FILE_ATTRIBUTE_NORMAL with any other flags - but you're using it with FILE_FLAG_BACKUP_SEMANTICS. To use FILE_FLAG_BACKUP_SEMANTICS, you have to get privileges for SE_BACKUP_NAME. You'll have to be an admin or a backup operator to do so. I can't imagine that you need that flag.
You can get the volume handle by opening "\\.\C:" (for example)...which is different than that handle to the root folder. I usually open it with GenericRead, but if all you need it for is for OpenFileById, you can specify 0 for access.
Also - adding object IDs to files isn't necessary - the file reference number (FRN) is the master file table identifier for the file - it's the "other" kind of ID you can pass in a FILE_ID_DESCRIPTOR. You can get it from an open file handle calling GetFileInformationByHandle - it's the nFileindexHigh and nFileIndexLow made into a long int. When you move a file, the FRN stays (only it's parent FRN changes). Also, when you rename a file, the FRN doesn't change. The benefit of using this over ObjectID is that you're not altering the volume in order to track a file...and you don't have to use DeviceIOControl - which is a bit of an interop bad dream.
One more thought - OpenFileByID didn't show up until Vista and Windows Server 2008. You're there, right?

Programatically get path of open Excel/Word-document

Is there any way to get the path of documents currently open in Microsoft Word and Microsoft Excel?
I know how I can fetch the PID and the window handle for the running instance of Excel or Word, but I have no idea how to get the path of the open document. Is it even possible?
I am thinking that an API would be nice, but I doubt it even exists. Perhaps some kind of plugin that sends information to my application is a possibility? (But if any other way is possible, I'd like to avoid having to install plugins everywhere.)
I would prefer to do it using C#, but anything that can be compiled into an executable is alright.
And just in case someone thinks this sounds fishy, it is not to be used for anything remotely illegal. It is for a data collection system at work.
There is an API called Office Interop... it is basically a COM based interface for MS Office... and can be used from C# .
For Excel you can get the current file including path via Application.ActiveWorkbook.FullName.
For Word you can get the current file including path via Application.ActiveDocument.FullName.

Unable to access file that is in use

I have written an Add-in for Windows Home Server Console that is supposed to copy and replace some files among other things.
The problem is that one file is already used by HomeServerConsole.exe and therefore I cannot replace it with another. I get "Cannot access file because it's being used by another process".
I'm not sure how to solve this. My first idea was to programmatically close HomeServerConsole.exe and lauch another simple program to do the replacing. How do I do that though?
Another idea was to somehow get HomeServerConsole.exe to unlock the file for me to do my thing and then handing it back to HomeServerConsole. But how?
I've also begun looking at Win32Api to solve the problem but haven't yet found a solution.
How would you go about solving it?
These methods require a reboot:
How to replace in-use files at Windows restart

Categories

Resources