Why is EnvDTE.ProjectItem.FileCodeModel = Nothing? - vb.net

I wrote a VSMacro (in VS2010) that parses a solution and adds try and catch statements to cpp files. It's been working fine up until now. I had to reformat my pc and reinstall VS2010 and now it crashes. Here's a snippet from the macro.
Sub Foo(ByVal file As EnvDTE.ProjectItem)
Dim fileCM As EnvDTE.FileCodeModel
fileCM = file.FileCodeModel
End Sub
I've checked to see if that file is a valid object, and it does point to a cpp file. But for some reason file.FileCodeModel = Nothing. Why would FileCodeModel be Nothing?

There are several scenarios in Visual Studio where a ProjectItem will not return a FileCodeModel object. For example if the ProjectItem points to a folder or a non-code file item in solution explorer.
Can you verify what the ProjectItem represents in this scenario. In particular
Is it pointing to a file?
What type of project is this in (Web, console app, etc ...)?

Usually, it happens when the project is not compilable. You need to try to build the solution first.

Related

VB solution runs in debugger but not with exe's in bin folder

I have a solution that use external dll's in vb, which works perfectly when I run it from vb in debugger. However when trying to execute the program from the exe files in the bin folder the program crashes with the release exe giving a "InvalidOperation" message and the debug exe gives the error message shown below.
The code segments are:
vb:
<DllImport("libjcom_exp-0.dll", Entrypoint:="jcreate")> _
Public Shared Function Jcreate() As IntPtr
End Function
Public Sub New()
objptr = Jcreate()
error_message = Jget_last_err(objptr)
Jset_rps(objptr, 40)
If error_message.Length <> 0 Then
Throw New System.Exception(error_message)
End If
End Sub
c++:
DLL_EXP void * __stdcall jcreate()
{
return new libjcom_exp;
}
Things that I have tried from searching around:
Disable anti virus
Compile without optimizations
Use dependency walker to check if all dll's are included in the bin folder
Confirm that the exe and all related files builds into the correct folder
Project is build as a x86 project
Someone else suggested that the 'jcreate' and 'get_last_error' functions executes at the same time where in the debug mode you individually step over single lines, which then does not trigger the problem.
The OS is Windows 10 and VB 2008.
Thank you in advance.
EDIT: I also used 'Run as administrator' on both exe's with the same result

Access to path is denied when trying to import from the client's desktop with SSIS

I'm creating a html page that will import an excel file in to a tracking system. On a button click event excel file is located / ssis package is fired / data imported then closed out. Thats the idea work flow. Problem is the excel file access is being denied before the package even executes
Here is the exact error :
I've tried :
excel file properties have been shared to everyone
identity impersonate set to true
hard coding the path
here is the VB code
Protected Sub bntExecute_Click(sender As Object, e As EventArgs) Handles btnExecute.Click
Dim app As Application = New Application()
Dim package As Package = Nothing
'Dim fileName As String = "C:\Users\Desktop\T. Bryant III\PTSID_Update_Template"'
Try
Dim fileName As String = Server.MapPath(System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName.ToString()))
FileUpload1.PostedFile.SaveAs(fileName)
package = app.LoadPackage("#C:\Users\Desktop\T.Bryant III\KitImport", Nothing)
'excel connection from package'
package.Connections("SourceConnectionExcel").ConnectionString = "provider=Microsoft.Jet.OLEDB.4.0data source =" + fileName + "Extended Properties = Excel 8.0"
'Execute the pakage'
Dim results As Microsoft.SqlServer.Dts.Runtime.DTSExecResult = package.Execute()
Catch ex As Exception
Throw ex
Finally
package.Dispose()
package = Nothing
End Try
End Sub
Thanks in advance or if there is an easier way to do this please let me know. The package when executing it in ssis works fine with its own connection manager etc.
A few things to try. If they don't work for you as permanent solutions, they should at least confirm that your code is working and you are dealing with a persmissions issue (which appears to be the case).
Move your file to the public folder (C:\Users\Public).
Run your application (or web browser) as an administrator (if applicable to your version of Windows).
If you are using a web browser, try using a different one.
If nothing else works, try pasting your code into a Windows Form Application.
If you still get the same error after trying all of this, it's time to take another look at your code. Remove the Try/Catch block to determine precisely which line is throwing the error. If you've tried hard coding, I'm guessing it's the SaveAs method. I'm not sure what class FileUpload1 is, but some SaveAs methods won't overwrite existing files unless you explicitly tell them to. Check the appropriate documentation and see if you don't need to pass a True value somewhere along with filename.
Update us with the results. At the very least, this should narrow down your problem and allow for a better diagnosis of it.

How to use "ReSharper.ReSharper_SilentCleanupCode" in a vs2010 macro?

I am trying to build a macro that formats all modified files before saving them.
Public Module ReformatAndSave
Sub SingleFile()
DTE.ExecuteCommand("ReSharper.ReSharper_SilentCleanupCode")
DTE.ActiveDocument.Save()
End Sub
Sub AllFiles()
For Each doc As Document In DTE.Documents
If Not doc.Saved Then
doc.Activate()
DTE.ExecuteCommand("ReSharper.ReSharper_SilentCleanupCode")
DTE.ActiveDocument.Save()
End If
Next
End Sub
End Module
This results in an error
Error HRESULT E_FAIL has been returned from a call to a COM component.
It works when I use this instead:
DTE.ExecuteCommand("ReSharper.ReSharper_CleanupCode")
I could live with this solution for a single file but choosing the profile when saving all files is annoying.
I use ReSharper 6.1.1000.82. This bug seems to be rather old: http://youtrack.jetbrains.com/issue/RSRP-179846
Is it possible to work around this bug by collecting all modified files and the execute the working CleanUpCode command once for all the files.
I can manually select many files and execute CleanUp on these files, manually. I would like to do this automatically on all modified files when saving them.
The solution is so simple.
All I had to do is replacing this
DTE.ExecuteCommand("ReSharper.ReSharper_CleanupCode")
with this
DTE.ExecuteCommand("ReSharper_SilentCleanupCode")
This doesn't work!
DTE.ExecuteCommand("ReSharper.ReSharper_SilentCleanupCode")

vb.net application works with files dragged onto the exe, but crashes if there's a space in the file's path

I'm developing an application in vb.net. You drag any type of file onto the exe, and a window pops up with some options for the file, then it saves the file to a different location, works some SQL magic, etc. It works great for the most part.
The only issue I've found is that if the path of the file contains any spaces, the application will crash immediately with the error window: http://i.stack.imgur.com/mVamO.png
I'm using:
Private filename as String = Command$
This is located right inside my form's class declaration, not within a sub/function.
Without this line, my program runs fine (although useless, without accessing the file).
I've also tried (I think this was it, I don't have the code with me at the moment):
Private filename as String = Environment.CommandLine
And it had the same issue.
So, in vb.net, is there a way to drag a file onto an exe and use that path name, even if there are spaces in the path name?
Windows will put double-quotes around the passed command line argument if the path to the dragged file contains spaces. Trouble is, you are using an ancient VB6 way to retrieve the argument, you see the double quotes. Which .NET then objects against, a double quote is not valid in a path name. Use this:
Dim path = Command$.Replace("""", "")
Or the .NET way:
Sub Main(ByVal args() As String)
If args.Length > 0 then
Dim path = args(0)
MsgBox(path)
'' do something with it..
End If
End Sub
If possible, do post your code as it's pretty much anything that can go wrong. Normally, after receiving CommandLine Arg, I would try to use a System.IO.File wrapper and use built-in mechanisms to verify file and then proceed with it further using IO as much as possible. If you are attempting to directly manipulate the file, then the spaces might become an issue.
In addition, there is a way to convert long file path + name to old DOS’s 8.3 magical file path + name. However, I’ll go into R&D after I see what you are doing in code.

Importing B.exe into A.exe an then run B.exe from A.exe

I am using Visual Basic 2008 and I have a question about it?
I have a.exe and b.exe ( the a.exe is an vbApp, and b.exe is an executable file ). Is it possible to include the b.exe into a.exe and then running it from a.exe? By, for example, importing the b.exe into vbProject and then running it without extracting it.
The question is a bit vague, but you can definitely do something along those lines, in several different ways.
First, you can certainly compile a separate EXE (I'll call it EXEA) into a VB Project (Call it EXEB). When user runs EXEB, it extracts the resource containing EXEA, saves it as a file (likely to the temp folder or someplace with WRITE rights) and then shells to EXEA.
Another possibility would be to compile external functionality into a DLL, call it DLLA, then compile that dll into a VB project (call it EXEB).
When user runs EXEB, it extracts the resource containing DLLA, storing it as a memory stream, then uses ASSEMBLY.LOAD to load the DLL from the memory stream (instead of from a file), and at that point can create objects from that dll, and use it as normal.
In both these cases though, it's probably better to simply compile the second EXE or DLL and include both in an MSI installation project.
More details in the question might help narrow down other possible solutions as well.
Try this :
1) Open project of B.exe.
2) Create a new module in project B, add "Sub Main" , then write this
Sub Main(Byval args() As String)
Dim X As New Form1 'replace Form1" with your real startup form
X.ShowDialog
End Sub
3) Open properties of that project B, and uncheck checkbox with name "Enable application framework". And change "Startup Object" to "Sub Main"
4) Compile and close project B.
5) Now open project of A.exe, and use following code to run B.exe in memory:
Dim tmpAssembly As System.Reflection.Assembly = System.Reflection.Assembly.Load(System.IO.File.ReadAllBytes("C:\b.exe"))'Replace "C:\b.exe" real b.exe path
Dim TmpMethod As MethodInfo = tmpAssembly.EntryPoint
If TmpMethod IsNot Nothing Then
Dim TmpObject As Object = tmpAssembly.CreateInstance(TmpMethod.Name)
Dim args() As String = Nothing
TmpMethod.Invoke(TmpObject , args)
End If
Me.Close()
CAUTION! You can run applications on memory only if you have done steps 1 and 2 with those applications. Otherwise you will get errors , which are not solved yet...