amma.NETtami

.NET walkabout
posts - 11, comments - 18, trackbacks - 0

venerdì 25 settembre 2009

L'incredibile vulnerabilità di SVN

C'è di che ridere (e sto ridendo parecchio), ma la cosa è MOLTO seria.

SmashingMagazine spiega per filo e per segno quale è il problema che affligge SVN e come questo generi non pochi grattacapi a coloro i quali non hanno pensato a come proteggersi.

Della serie: cerchi di blindare tutta la casa, ma hai dimenticato che la porta principale va SEMPRE chiusa a chiave.

posted @ venerdì 25 settembre 2009 13:51 | Feedback (0) | Filed Under [ Sicurezza ]

venerdì 8 maggio 2009

Editare file di Excel 2007 da VBA o VB6

Interessante articolo su come editare il formato OPENXML di Excel 2007 utilizzando il buon vecchio VBA o, per estensione, il sempre valido Visual basic 6.

http://www.jkp-ads.com/articles/Excel2007FileFormat02.asp

posted @ venerdì 8 maggio 2009 10:44 | Feedback (1) | Filed Under [ Visual Basic VBA VBA ]

venerdì 27 marzo 2009

How to zip a file using Visual Basic 6 and the Windows Shell

When it was a matter of zipping a file, I always choosed the easy way: find a thirdy part activex or dll which could do the job for me.

This time, I wanted to find my way to zip a file using Visual Basic 6 (or Visual Basic for Application - aka VBA) and the windows shell. The consderation came from the fact that, since Windows XP,  the support of zipping-unizzping files is a native feature of the OS.

After a long search and multiple fixes, I finally came up with a stable solution which I'm going to quickly explain and post right here. The source code is a mix of pieces of code collected here and there. the only att

Attached to the post it's a zip file containing a working example. It zips the files contained in the testFolder directory.

Click here to download the zip.

I'm going to post just the clsZip class I used to support my zipping project.

clsZip.cls
Option Explicit

Private objShell As Object
Private mvarZipFileName As String

Const FOF_NOCONFIRMATION = &H14

Private Sub Class_Initialize()
Set objShell = CreateObject("Shell.Application")
End Sub

Private Sub Class_Terminate()
Set objShell = Nothing
End Sub

Public Property Let ZipFileName(ByVal vData As String)
    mvarZipFileName = vData
End Property

Public Property Get ZipFileName() As String
    ZipFileName = mvarZipFileName
End Property


Private Sub CreateEmptyZip(sPath)

    Dim strZIPHeader As String
    
    Dim fso As Object
    
    strZIPHeader = Chr$(80) & Chr$(75) & Chr$(5) & Chr$(6) & String(18, vbNullChar)
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    With fso
        .CreateTextFile(sPath).Write strZIPHeader
    End With
    
    Set fso = Nothing
    
End Sub

Public Function AddFilesToZip(sFileNames() As String) As Boolean

Dim i As Long
Dim iCount As Long

    On Error GoTo AddFilesToZip_Error
    
    CreateEmptyZip mvarZipFileName
    
    On Error Resume Next
    For i = LBound(sFileNames) To UBound(sFileNames)
        objShell.Namespace("" & mvarZipFileName).CopyHere "" & sFileNames(i), FOF_NOCONFIRMATION
        
        iCount = objShell.Namespace("" & mvarZipFileName).items.Count
    
        Do Until iCount = i + 1
            Sleep 100
            iCount = objShell.Namespace("" & mvarZipFileName).items.Count
        Loop
        
    Next
        
    On Error GoTo 0
    Exit Function

AddFilesToZip_Error:

    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure AddFilesToZip of Modulo di classe clsZip"

End Function
Public Function UnzipToFolder(sFolderName As String) As Boolean

    objShell.Namespace("" & sFolderName).CopyHere objShell.Namespace("" & mvarZipFileName).items

End Function

Technorati tags: , , ,

posted @ venerdì 27 marzo 2009 12:40 | Feedback (0) | Filed Under [ Visual Basic VBA ]

lunedì 10 novembre 2008

Excel: convenzione di chiamata dll non valida

Una mattina persa a cercare di capire quale potessere essere il mistero legato alla convenzione di chiamata dll non valida.

La soluzione, come spesso accade, sta nel cercare di arrivare all'unica verità possibile, ovvero che il cucchiaio non esiste... Esportare tutti i moduli bas dal progetto vba, eliminarli e reinserirli. Eseguire la compilazione del progetto dall'interno dell'ambiente di sviluppo et voilà... Les jeux sont fait!

posted @ lunedì 10 novembre 2008 23:09 | Feedback (0) | Filed Under [ VBA ]

lunedì 20 ottobre 2008

Contrarre una selection (range) in Excel

Semplice funzione per Excel che consente di contrarre di n righe una selection (per intenderci, un range di celle) .

Risulta utile anche per spostarsi in su od in giù di n righe se la selection corrente è una singola cella.

Function CropSelection(Rows As Long) As Boolean
 
Dim sSelection As String
Dim iDollarPosition As Long
Dim sRow As String
Dim lRow As Long

    On Error GoTo CropSelection_Error

    sSelection = Selection.Address()
    
    iDollarPosition = InStrRev(sSelection, "$")
    
    sRow = Mid(sSelection, iDollarPosition + 1)
    lRow = CLng(sRow) - Rows
    sRow = Mid(sSelection, 1, iDollarPosition - 1) & CStr(lRow)
    
    Range(sRow).Select

    On Error GoTo 0
    CropSelection = True
    Exit Function

CropSelection_Error:

    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure CropSelection of Modulo Modulo1"

End Function

posted @ lunedì 20 ottobre 2008 23:42 | Feedback (0) | Filed Under [ VBA ]

mercoledì 23 aprile 2008

Clausola TOP n

SQL Server:
SELECT TOP 10 product, descr, email
FROM products
 
ORACLE:
SELECT product, descr, email
FROM products
WHERE ROWNUM <= 10

 
MySQL:
SELECT product, descr, email
FROM products
LIMIT 10

posted @ mercoledì 23 aprile 2008 21:48 | Feedback (0) | Filed Under [ T-SQL Oracle MySQL ]

giovedì 15 novembre 2007

How to format a value using T-SQL: pad left

SQL server has no built-in format functions. Here is a user defined function which left-pad a varchar value with a variable length string.

CREATE FUNCTION [dbo].[PadString]

(@Seq varchar(16),
@PadWith char(1),
@PadLength int
)

RETURNS varchar(16) AS

BEGIN

declare @curSeq varchar(16)

SELECT @curSeq = ISNULL(REPLICATE(@PadWith, @PadLength - len(ISNULL(@Seq ,0))), '') + @Seq

RETURN @curSeq

END

Testing the function:

SELECT dbo.PadString ('8', '0', 5)

SELECT dbo.PadString ('abc', '*', 12)

SELECT dbo.PadString ('abc', '0', 7)

 

Here are the results:

                
----------------
00008

(1 row(s) affected)

                
----------------
*********abc

(1 row(s) affected)

                
----------------
0000abc

(1 row(s) affected)

---------------------------------------------------------

TAGS: sql server format string pad left

posted @ giovedì 15 novembre 2007 09:55 | Feedback (9) | Filed Under [ T-SQL ]

domenica 7 ottobre 2007

OLEDB and text files

This is a very quick example about OLEDB and read a text (.txt) file using Visual Basic 6.
The text file must be formatted as a simple CSV file with a field separator. Something like this

Test.txt

a;1;Test
b;2;Test
c;3;Test
d;4;Test

Supposing "Test.Txt" is stored in the root of the C: harddrive, the code will look like

----------------------------------------

Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Set cn = CreateObject("ADODB.Connection")
cn.Open
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\;Extended Properties=""text;HDR=No;FMT=Delimited'"""
Set rs = CreateObject("ADODB.Recordset")
rs.Open
"SELECT * FROM Test.txt", cn, 0, 1, 1
While Not rs.EOF
    Debug.Print rs.Fields(
0).Value, rs.Fields(1).Value, rs.Fields(2).Value
    rs.MoveNext
Wend
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing

----------------------------------------

Take a look to the connection string

"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\;Extended Properties=""text;HDR=No;FMT=Delimited'"""

the Data Source=c:\; is the key. If you plan to store your text file in a different folde, let's say "c:\documents and settings\Auser\Documents\myTestFiles\", you are required to change the connection string this way:

"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\documents and settings\Auser\Documents\myTestFiles\;Extended Properties=""text;HDR=No;FMT=Delimited'"""

posted @ domenica 7 ottobre 2007 21:00 | Feedback (0) | Filed Under [ Visual Basic OLEDB ]

domenica 2 settembre 2007

ShowModalDialog and ASP.NET, a full working example

Here is a simple working example explaining how to use ShowModalDialog and asp.net with Visual Studio 2005.

Start with default.aspx, which has a single button. Once clicked, it will popup a modal window which will show a single button labeled "Close me!".

The postback will redirect the user to a new page, whose purpose is to close the modal window and return the string value '1' which will be evalueted by the parent window using the returnValue property.

If returnValue is '1' then the form will be submitted to itself, just to show how to reuse local values and force a timer label to refresh

Download the example from here

posted @ domenica 2 settembre 2007 22:54 | Feedback (4) | Filed Under [ ASP.NET ShowModalDialog ]

lunedì 27 agosto 2007

Running custom VbScript code from asp

Running custom VbScript code from asp

I was in the need to run some custom VbScript code from an asp page, so I went through the problem and found a very simple solution.
A common solution is to use the Microsoft ScriptControl.
Here is a very simple example running a piece of code stored in a text file.

default.asp

<%@language=VBSCRIPT%>
<%

dim sFile : sFile = ""

sFile
= LoadFile("MyCustomScript.txt")

if len(sFile) > 0 then
    Response.Write
"The result is: " & ExecScript(sFile)
end if

Function ExecScript(sScript)

       
dim sc
       
'Create the Script Control object
        set sc = server.CreateObject("scriptcontrol")
       
        sc.Language
= "VBSCRIPT"
        sc.UseSafeSubset
= false
        sc.Reset
       
       
'add the custom script to the ScripContro object
        sc.AddCode sScript
       
'run the code and evaluate the result of the "CustomScript" function
        ExecScript = sc.Eval("CustomScript")
       
       
set sc = nothing

End Function

Function LoadFile(sFileName)
   
Dim objFSO
   
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

   
Dim objTextStream
   
dim strFileName
   
dim lsTemp

    strFileName
= server.MapPath(sFileName)
   
const fsoForReading = 1

   
If objFSO.FileExists(strFileName) then
       
'The file exists, so open it and output its contents
        Set objTextStream = objFSO.OpenTextFile(strFileName, fsoForReading)
        lsTemp
=  vbNullString & objTextStream.ReadAll & vbNullString
        objTextStream.Close
       
Set objTextStream = Nothing
   
Else
       
'The file did not exist
        Response.Write strFileName & " was not found."
   
End If

   
'Clean up
    Set objFSO = Nothing
    LoadFile
= lsTemp
end function

%
>

MyCustomScript.txt

function CustomScript()
   
'Let's calculate the area of a rectangle
    dim dHeight : dHeight = 10
   
dim dWidth : dWidth = 20
    CustomScript
= cstr(dHeight * dWidth)
end function

This is a very easy example of how to use the Microsoft ScriptControl in an asp page.
A more realistic task would be, for example, store the script in a database, get the scripts from there and run 'em like exposed above.

posted @ lunedì 27 agosto 2007 21:25 | Feedback (3) | Filed Under [ ASP VbScript ]

Powered by:
Powered By Subtext Powered By ASP.NET